views:

90

answers:

7

Hi,

what is the right way to determine if an object has one class OR another one? The following is appearantly wrong..

if ($('#menu-item-49').hasClass('current-menu-item' || 'current-menu-parent') ) {
  $('ul.sub-menu ').css('display', 'block');
}

Thanks!

A: 

The following is what you've in mind:

var menuItem49 = $('#menu-item-49');
if (menuItem49.hasClass('current-menu-item') || menuItem49.hasClass('current-menu-parent')) {
    // ...
}
BalusC
yep thats it! thanksill check your answer in a bit...
nwindham
+4  A: 

You could use is instead?

if ($('#menu-item-49').is('.current-menu-item, .current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}

Check the current matched set of elements against a selector and return true if at least one of these elements matches the selector.

Beats having to use multiple hasClass queries, which is the alternative:

if ($('#menu-item-49').hasClass('current-menu-item') || 
    $('#menu-item-49').hasClass('current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}
GenericTypeTea
That's a nice one!
BalusC
Sure is! Better than writing hasClass() || hasClass || hasClass over and over.
GenericTypeTea
+1 very nice. You posted it before I did :)
Vivin Paliath
+1 for the slightly more elegant solution
HurnsMobile
Did you mean to place the comma inside the quotes? `'.current-menu-item, .current-menu-parent'` Currently the second argument is ignored.
patrick dw
@patrick - Thanks for spotting that!
GenericTypeTea
A: 

So close!

Try:

if ($('#menu-item-49').hasClass('current-menu-item') || $('#menu-item-49').hasClass('current-menu-parent') ) {
  $('ul.sub-menu ').css('display', 'block'); 
}
HurnsMobile
A: 
if ($('#menu-item-49').hasClass('current-menu-item') || $('#menu-item-49').hasClass('current-menu-parent') ) {
  $('ul.sub-menu ').css('display', 'block');
}

Have you tried the following?

Trefex
+1  A: 

You cannot use the || (or) operator within hasClass. Try this:

if ($('#menu-item-49').hasClass('current-menu-item') || 
    $('#menu-item-49').hasClass('current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}

Or you can do this if that's too verbose:

var $menuItem = $('#menu-item-49');
if ($menuItem.hasClass('current-menu-item') || 
    $menuItem.hasClass('current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}

Or even better:

var $menuItem = $('#menu-item-49');
if ($menuItem.is('.current-menu-item, .current-menu-parent')) {
  $('ul.sub-menu ').css('display', 'block');
}
Vivin Paliath
In your last version, shouldn't the comma be inside the quotes? `'.current-menu-item,.current-menu-parent'` I'm pretty sure `is()` only accepts 1 argument, but the other answer with this solution did the same thing, so now I'm starting to doubt myself. :o)
patrick dw
Ugh, my bad. You're right! Will fix this.
Vivin Paliath
A: 

Do it like this instead, separating it in two bits:

if ($('#menu-item-49').hasClass('current-menu-item') || $('#menu-item-49').hasClass('current-menu-parent') ){
   $('ul.sub-menu ').css('display', 'block');
}

Hope this helps you

Marcos Placona
A: 

You can also use .is() to check for any class beginning with current-menu- by using:

if ( $('#menu-item-49').is('[class]^="current-menu-"') ) {
    //49 is the current menu!
}

More attribute selectors like ^= may be found here.

jasongetsdown