Using Jquery to append a li to an ul where oid=xxx

1.4k Views Asked by At

I am trying to append a li to only the UL which has a certain oid value set like this

$('ul.sortablespm oid=' + data).append('<li></li>');

It works fine without the oid parameter, but then adds the li to all ULs on the page..

2

There are 2 best solutions below

0
On BEST ANSWER

Use the appropriate attribute selector syntax, that is, [name="value"]:

$('ul.sortablespm[oid="' + data + '"]').append('<li></li>');
0
On

Here is a working example of what I believe you are looking for.

http://jsfiddle.net/chapmanc/wU6Vs/

If the ul object has a unique id you can simply use that as your selector and append an item to that object.

For example a ul with an id of myList:

<ul id="myList">
    <li>a</li>
    <li>b</li>
    <li>c</li>
</ul>​

And a function to append to that object using the unique selector:

$('#myList').append('<li>d</li>');​

This appends a li item to the myList object. Notice the '#' character in front of myList when using an id selector.