..checkbox....head.. ...">

Jquery hasClass for multiple classes

I have,

<tr valign="top"> <th class="chkCol fixed">..checkbox..</th> <th class="fixed">..head..</th> </tr> 

Now in jQuery I call a function like

 if($(this).hasClass("fixed")){ .... } 

If I call $(this).hasClass("fixed") , then I only need to set the head flag, and it works fine in JQuery 1.4.2, but now I upgraded to jquery 1.6.1. Now I get a checkbox inside if condition.

Please, help,

Thanks in advance

+6
source share
5 answers

I would be very surprised if jQuery 1.4.2 is wrong. jQuery 1.4.2 is not mistaken. hasClass("fixed") must be true in both cases in all versions of jQuery. Here is an example using v1.6.1, and the same example using v1.4.2. Both work fine.

If you want to verify that the β€œfixed” class is only for an element that is easier to do without jQuery by going directly to the reflected property:

 if (this.className === "fixed") { // ... } 

className is a property that reflects the class attribute, and like the class attribute, it is a space-delimited string.

The foregoing, of course, will not succeed if the element has another class on it, for example, "fixed something-else".

If you specifically want to verify that it has a "fixed" and no "chkCol", then you must do this on purpose:

 var $this = $(this); if ($this.hasClass("fixed") && !$this.hasClass("chkCol")) { // ... } 

If you did this in the selector (instead of checking the element that you already had), you can use the selector ".fixed:not(.chkCol)" . But although you can do this with an element that you already have is , it is unnecessarily expensive (jQuery must parse the selector).


Refresh . You said you tried the && !$this.hasClass("chkCol") thing, and it didn't work. This means that something earlier in the if condition already defined the result of the expression as a whole, and therefore the result of the !$this.hasClass("chkCol") does not matter (and the circuit does not start at all).

Example:

 var x = 1; if (x == 1 || $this.hasClass("fixed") && !$this.hasClass("chkCol")) 

In this case, since x - 1 , nothing to the right of || not even visible; the value of the expression is already true .


Update 2 :

You said you were using hasClass , but in a comment on another answer, you said:

If I use $(this).attr('class') == "fixed" then it does not enter at all if the condition

I do not think that you are actually doing this:

 if ($(this).attr('className') == "fixed") { // ... } 

Note that "className", not "class". If this is what you are doing, then it will not work on 1.6.1, but it will be on 1.4.2. There is no attribute called "className"; there is an attribute called "class" and a property of "className". jQuery 1.6.x distinguishes between attributes and properties.

My code above using hasClass will work on all versions. Or change your code to use attr("class") rather than attr("className") .

Here's an example v1.4.2 showing attr("className") working (which it shouldn't). Here's an example v1.6.1 showing attr("className") does not work (which is correct). Both versions show the correct way to get the "class" attribute.

+9
source

You can do it:

 if ($(this).attr('className') === 'fixed') 

or more:

 if ($(this).not('.chkCol').hasClass('fixed')) 

I think it would be better to make sure that the elements of "chkCol" never get a "fixed" class in the first place.

edit - eternally perceptive @T. J. Crowder correctly pointed out that ".attr ()", like jQuery 1.6, is the wrong way to get property values ​​from elements. There, the degree of the angelic dance pin is involved in explaining why this is the case, but suffice it to say that you are now using:

 if ($(this).prop('className') === 'fixed') 
+3
source

It seems the update fixed the error, then :) hasClass("fixed") should definitely be true for both th elements here.

As for your problem, either explicitly exclude the chkCol class:

 if ($(this).hasClass("fixed") && !$(this).hasClass("chkCol")) { 

or find other criteria that identify the item you are looking for (tag name, identifier, other highlighted class, etc.).

+1
source

Well, then this must be a bug in jQuery 1.4.2, because obviously <th class="chkCol fixed">..checkbox..</th> has a class "fixed".

If you want to get an element with a single "fixed" class, you could do

 ($(this).attr('class') == "fixed") 
+1
source

Maybe you should use attr () instead:

 if($(this).attr("class") == "fixed"){ .... } 
+1
source

Source: https://habr.com/ru/post/890063/


All Articles