views:

96

answers:

1

This is a simple syntax question.

I have a ul saved as a variable like this:

$list = $("#city_list");

I am later removing some items from the list with this code:

$('ul#city_list li#city_' + $id).remove();

How can I do that using the $list variable I created earlier so that I get something like this:

$list.('li#city_' + $id).remove();
+2  A: 

Use find

$list.find('li#city_' + $id).remove();

Never prefix an ID with a tag name

. So change your code to

$list.find('#city_' + $id).remove();

Read

jQuery performance Rules

rahul
Good point about id's. An id is always supposed to be unique, so it's the only thing you need for selection. You can't get any more specific than that.
Frank DeRosa
jQuery would optimise this for you behind the scenes anyway... But still, it's a good rule to follow.
J-P