1

I have built a <ul> list and it is being populated with <li id="subsitem"> from a jQuery function. I am trying to build a click function so that whenever one of those <li>'s is clicked, it disappears. I'm using the slideUp() function.

Here is my code:

$("#subsitem").click(function() {
  // Act on the event
  $(this).slideUp();
}); 

This doesn't work, yet when i change it to

$("li").click(function() {
  // Act on the event
  $(this).slideUp();
}); 

It works, but the only problem then is it works on all my <li>s. Anyone any tips?


update

Here is the code that is being used inside an .each()

$("#subs").append("<li><a href='#' class='subsitem'><div class='favicon'><img src='icon.png' width='16' height='16'></div>title</a></li>");
ChrisMJ
  • 1,620
  • 4
  • 21
  • 27

3 Answers3

4

The problem is the first one is identifying by ID, which should be unique. You can change it to a class, and add the class to each li, to make it work.

$(".subitem").click(function() {
    // Act on the event
    $(this).slideUp();
}); 
Robert
  • 21,110
  • 9
  • 55
  • 65
  • i know the code works, because if i create another list on the same page with the li and class, then the code works fine, its almost like my first ul list is bugged – ChrisMJ Aug 17 '10 at 02:06
  • 1
    You need to append `$("#subs").append("
  • title
  • ");` for my code to work. The difference being the `
  • ` having subitem as the class.
  • – Robert Aug 17 '10 at 02:24