tags:

views:

52

answers:

2

I need to add the click event to all the li's inside a div which has id to the li.

The following script changes for all the li's:

   <ul>
    <li>
    <h4>My Header</h4></li>
    <li id="myli1">My li 1</li>
    <li id="myli2">My li 2</li></ul>


        $("#mydiv li").live('click', function(e) {
            alert($(this).attr("id"));
          });

I need to add the click events for only the li which has the id attribute.

+6  A: 

This should do it:

    $("#mydiv li[id]").live('click', function(e) {
        alert($(this).attr("id"));
      });
Greg
that worked greg. thanks
Prasad
A: 

You can take the solution of @ Greg or you can select the types of Id's. see jQuery Selector

$("#mydiv li[id^='myli']").live('click', function(e) {
        alert($(this).attr("id"));
});
andres descalzo