Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery determine if ul has class OR another one

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!

like image 909
nwindham Avatar asked Jul 07 '10 16:07

nwindham


1 Answers

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'); } 
like image 89
djdd87 Avatar answered Sep 19 '22 01:09

djdd87