I am having,
<tr valign="top">
<th class="chkCol fixed">..checkbox..</th>
<th class="fixed">..head..</th>
</tr>
Now in Jquery I am calling a function like,
if($(this).hasClass("fixed")){
....
}
If I call $(this).hasClass("fixed"), then I need to get only head not checkbox and that is working perfect in Jquery 1.4.2 but now I updated to jquery 1.6.1.
Now I am getting checkbox inside if condition.
Please Help,
Thanks in advance
I’d be very surprised if jQuery 1.4.2 gets this wrongjQuery 1.4.2 does not get this wrong.hasClass("fixed")should be true in both cases, in all versions of jQuery. Here’s an example using v1.6.1, and the same example using v1.4.2. Both work fine.If you want to check that “fixed” it’s the only class on an element, that’s more easily done without jQuery, by going directly to the reflected property:
classNameis a property reflecting the “class” attribute, and like the “class” attribute it’s a space-delimited string.The above will fail, of course, if the element has any other class on it as well, like “fixed something-else”.
If you specifically want to check it has “fixed” and doesn’t have “chkCol”, then you have to do that on purpose:
If you were doing this in a selector (rather than checking an element you already had) you could use the selector
".fixed:not(.chkCol)". But although you can do that with an element you already have usingis, it’s unnecessarily expensive (jQuery has to parse the selector).Update: You’ve said that you’ve tried the
&& !$this.hasClass("chkCol")thing and it hasn’t worked. That means that something earlier in theifcondition has already determined the outcome of the expression as a whole, and so the result of the!$this.hasClass("chkCol")part makes no difference (and isn’t getting run at all, as JavaScript short-circuits expressions).Example:
In that case, because
xis1, nothing to the right of the||is even looked at at all; the expression’s value is alreadytrue.Update 2:
You’ve said you’re using
hasClass, but in a comment on another answer you said:I don’t suppose what you’re actually doing is this:
Note that’s “className”, not “class”. If that’s what you’re doing, that will not work on 1.6.1 but it will on 1.4.2. There is no attribute called “className”; there’s an attribute called “class” and a property called “className”. jQuery 1.6.x differentiates between attributes and properties.
My code above, using
hasClass, will work on all versions. Or change your code to useattr("class")rather thanattr("className").Here’s a v1.4.2 example showing
attr("className")working (which it shouldn’t). Here’s a v1.6.1 example showingattr("className")not working (which is correct). Both versions show the correct way to get at the “class” attribute.