Skip to content Skip to sidebar Skip to footer

Jquery If Has() Selector Do Stuff

  • text
http://jsfiddle.net/wZ8MC/2/ jQuery(document).ready(function() { if (jQuery('#bad-drifting').has('em'))

Solution 1:

That's because has returns a jQuery object and an object is a truthy value in JavaScript, you should use length property:

if (jQuery('#bad-drifting').has('em').length) {

Solution 2:

You need to check for length because jQuery('#bad-drifting li:has(em)') returns a jQuery object which will be always truthy.

if (jQuery('#bad-drifting li:has(em)').length) { // .has('em')

Solution 3:

if (jQuery('#bad-drifting').has('em')[0]) { //do Stuff

Solution 4:

I ended up doing like this:

if (jQuery('#bad-drifting em').length > 0) {
    console.log('we have some errors');
}   

Solution 5:

Check the length of your jQuery "collection":

if (jQuery('#bad-drifting li:has(em)').length > 0) { // There is at least one element

otherwise it will always be true, because what jQuery returns is always truthy.

Post a Comment for "Jquery If Has() Selector Do Stuff"