views:

88

answers:

3

Hi, the script below makes the page fade out upon clicks on links that have class="nav".

I would like to have the script ALSO activated in the same way for links that have class="home".

How would would do this? Is there such a thing as $('.nav & .home')??

Thanks

<script type="text/javascript">
$('.nav').click(function(){
  var url = $(this).attr('href');

  $('.mask').fadeIn('medium', function(){
   document.location.href = url;
  })
  return false;
 });

});
+10  A: 

You can simply use commas to specify multiple selectors:

$('.nav, .home').doSomethingToBoth();

A lesser known alternative is to use add:

$('.nav').doSomethingToNavs().add('.home').doSomethingToBoth();

I should also add that doing .class selectors are slow. If you know that only <a> elements are going to have the .nav and .home classes, you should write your selector like this:

$('a.nav, a.home').click(...);

Without this jQuery will be forced to look at every single element in the document to see if it has the class you specified, as opposed to only looking for <a> elements with the native getElementsByTagName and THEN looking to see if they have the class specified. This is just a good practice to keep in mind.

Paolo Bergantino
Good tip about the classes.
ScottE
+1  A: 

$('.nav, .home')...

ScottE
A: 
$(".nav, .home")
Julio Greff