jQuery syntax question : ul as variable, remove item?
-
06-07-2019 - |
Question
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();
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow