jQuery selectors to get array of nodes

Issue

I have the following HTML:

<ul class="main-navigation">
  <li class="left">&nbsp;</li>
  <li class="normal">Home</li>
  <li class="normal">Internet</li>
  <li class="normal">Movies</li>
  <li class="normal">Music</li>
  <li class="normal">Documents</li>
  <li class="normal">Windows</li>
  <li class="right">&nbsp;</li>
</ul>

What I want to do is, get the array of <li> elements which have the "normal" class and loop through them and add event listeners.

What jQuery code will return the array? I tried different combinations like:

$('.main-navigation.normal')
$('.main-navigation li .normal')

How can I get that array of nodes? How can I loop through them and add events?

Solution

$('.main-navigation .normal') or $('.main-navigation li.normal')

Using $('.main-navigation li .normal') you will select nodes inside li with the class .normal and with the $('.main-navigation.normal') you will select the element with both .main-navigation and .normal

Answered By – janhartmann

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published