-1

I have this selector:

if($('body').hasClass('page-modellen', 'page-modellen-detail')){

But this is not working. I want. When the page hass class page-modellen or page-modellen-detail than he must doing that code. How can i fix that?

Thanks for helping

Mike Vierwind
  • 1,482
  • 4
  • 23
  • 44

6 Answers6

4

You can use .is()

if($('body').is('.page-modellen, .page-modellen-detail')){
    console.log('x')
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

.hasClass(className) can't hold multiple className.

I used .is() selector for this.

if($('body').is('.page-modellen, .page-modellen-detail')){

Check this JSFiddle

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • Please share the reason for downvotes. So that I can learn/rectify right? – Praveen Oct 14 '13 at 12:26
  • 2
    `is()` can't handle multiple arguments. – VisioN Oct 14 '13 at 12:27
  • 1
    @mplungjan, that's a multiple selector in a single argument, not multiple arguments. – Frédéric Hamidi Oct 14 '13 at 12:37
  • @VisioN thanks, but I have a doubt, how this worked `$('body').is('.page-modellen', '.page-modellen-detail')` What I understand it look for the first parameter and negalected the second one. Please correct me if I meant wrong – Praveen Oct 14 '13 at 12:38
  • 1
    @user, you're right, passing multiple arguments to `is()` is not supported, and only the first one will be taken into account. Passing a multiple selector in a single argument will work, on the other hand. – Frédéric Hamidi Oct 14 '13 at 12:39
  • 1
    @user1671639 Yep, in your case `.is` took into consideration the first argument and skipped the rest. It gave you the correct result since the element indeed has `"page-modellen"` class. – VisioN Oct 14 '13 at 12:40
  • 1
    @FrédéricHamidi `window.console && console.log($('body.page-modellen, body.page-modellen-detail').length);` // returns 1 from @mplungjan gave an insight. Thanks for the clarification – Praveen Oct 14 '13 at 12:43
  • 2
    @mplungjan VisioN is correct. 'Arguments' means functional arguments, i.e. `.is('.class1', '.class2')` vs. `.is('.class1, .class2')`. Read carefully [Frédéric's comment](http://stackoverflow.com/questions/19360014/more-than-1-selector#comment28686152_19360049). – VisioN Oct 14 '13 at 12:50
  • 2
    @mplungjan My first comment was referring to the very first revision of the answer: http://stackoverflow.com/revisions/19360049/1, which had a problem with multiple arguments, as we discussed. – VisioN Oct 14 '13 at 13:14
2

check here

jQuery OR Selector?

Your code should be:

if($('body').is('.page-modellen, .page-modellen-detail')){
Community
  • 1
  • 1
mucio
  • 7,014
  • 1
  • 21
  • 33
1

Ever heard about ||? :)

if ($('body').hasClass('page-modellen') || $('body').hasClass('page-modellen-detail'))
Elon Than
  • 9,603
  • 4
  • 27
  • 37
1

So many answers, many ways to answer this.

Let me add one more that wasn't answered here yet:

if ($('body.page-modellen, body.page-modellen-detail').length > 0)
{
    // do stuff
}
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
0
if($('body').hasClass('page-modellen')|| $('body').hasClass('page-modellen-detail')){
  // do stuff 

}

reference JQuery .hasClass for multiple values in an if statement

Community
  • 1
  • 1
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55