kirankumar
kirankumar

Reputation: 197

How can i access children using parent id

I have this structure of html. To present the list of two different sets. and i must handle the click event differently.

<div id='nodelist1'>
<ul>
 <li class='nodeelem'>first node
    <ul>
      <li class='nodeelem'>second node
         <ul>
           <li class='nodeelem'>third node</li>
         </ul>
      </li>
     </ul>
   </li>
</ul>
</div>


<div id='nodelist2'>
<ul>
 <li class='nodeelem'>first node
    <ul>
      <li class='nodeelem'>second node
         <ul>
           <li class='nodeelem'>third node</li>
         </ul>
      </li>
     </ul>
   </li>
</ul>
</div>

I have to access the nodes using div id

$('#nodelist1 li.nodeelem').click(handler);
$('#nodelist2 li.nodeelem').click(handler2);

Is this rightway to access children clicks???

Upvotes: 0

Views: 65

Answers (1)

David Hellsing
David Hellsing

Reputation: 108520

You forgot the hash # for ID selectors (although you corrected this in your edit):

$('#nodelist1 li.nodeelem').click(handler);
$('#nodelist2 li.nodeelem').click(handler2);

Tip: you can make the event more effective by using on() instead for event delegation:

$('#nodelist1').on('click', '.nodeelem', handler);
$('#nodelist2').on('click', '.nodeelem', handler2);

Upvotes: 4

Related Questions