views:

65

answers:

3

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?

+3  A: 

You want to use jQuery.each

Update

This is untested, but try toggleClass:

jQuery("#main-navigation li.normal").toggleClass('newClass');
Steven
+1 Hi, I wanted to mark both your and meep's reply as answers. I can't mark two! Both of you answered separate parts of my question. Thanks! :)
Senthil
$("#main-navigation li").attr("class","normal") will set every li elements class to normal, not filter it.
Corey Hart
Can you explain that code? In jQuery, I usually get confused after the second DOT (.) !
Senthil
thanks. li.normal is also an option, and then remove .attr(), keeping .each().
Steven
Oh! What I want to do is, set the class to something else on mouseover and set it back to normal on mouseout
Senthil
Me too. But I think it basically says, take all li elements with attribute 'normal', and lop throug them.
Steven
And I want to do it for all the elements that are inside that <ul> and have 'normal' class iniatially. Basically I am trying to get a 'highlight-menuitem-on-hover' logic.
Senthil
If you only want to change color or have underline, just do it in CSS and not by using jQuery.
Steven
+7  A: 

$('.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

meep
+1  A: 

To add events, you don't need to loop through them. jQuery will add the event to all of the elements returned with the selection. For example:

$("ul.main_navigation li.normal").hover(function(){
    //what happens on mouseover
    $(this).attr('class', 'highlighted');
}, 
function(){
    //what happen on mouseout
    $(this).attr('class', 'normal');
}
);

Or you can use jQuery.each to loop through them, say if you need to add them to an array or something like this:

$("ul.main_navigation li.normal").each(function(){
    //this iterates through li.normal one by one
});
munch
Hi. I didn't know that! Thanks... Now your reply is the third one I want to mark as an answer! :)
Senthil
And inside the function(){}, I set the class like `this.attr('class','highlighted');` ??
Senthil
I edited the first bit of code in the answer. Saw you were trying to do a hover affect...
munch
I put a jquery selector around this, like $(this).attr('class', 'highlighted');
munch
$(this) worked. I have the hover effect working. Thanks :)
Senthil