In Mootools, I'd just run if ($('target')) { ... }
. Does if ($('#target')) { ... }
in jQuery work the same way?
This question is related to
javascript
jquery
jquery-selectors
firstly create a function:
$.fn.is_exists = function(){ return document.getElementById(selector) }
then
if($(selector).is_exists()){ ... }
no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length
if ( $('#someDiv').length ){
}
if you used:
jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }
you would imply that chaining was possible when it is not.
This would be better
jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }
jQuery.fn.exists = function(selector, callback) {
var $this = $(this);
$this.each(function() {
callback.call(this, ($(this).find(selector).length > 0));
});
};
Alternatively:
if( jQuery('#elem').get(0) ) {}
no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length
if ( $('#someDiv').length ){
}
Yet another way:
$('#elem').each(function(){
// do stuff
});
Alternatively:
if( jQuery('#elem').get(0) ) {}
if you used:
jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }
you would imply that chaining was possible when it is not.
This would be better
jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }
jQuery.fn.exists = function(selector, callback) {
var $this = $(this);
$this.each(function() {
callback.call(this, ($(this).find(selector).length > 0));
});
};
For me .exists
doesn't work, so I use the index :
if ($("#elem").index() ! = -1) {}
Yet another way:
$('#elem').each(function(){
// do stuff
});
firstly create a function:
$.fn.is_exists = function(){ return document.getElementById(selector) }
then
if($(selector).is_exists()){ ... }
I think most of the people replying here didn't quite understand the question, or else I might be mistaken.
The question is "how to check whether or not a selector exists in jQuery."
Most people have taken this for "how to check whether an element exists in the DOM using jQuery." Hardly interchangeable.
jQuery allows you to create custom selectors, but see here what happens when you try to use on e before initializing it;
$(':YEAH');
"Syntax error, unrecognized expression: YEAH"
After running into this, I realized it was simply a matter of checking
if ($.expr[':']['YEAH']) {
// Query for your :YEAH selector with ease of mind.
}
Cheers.
Alternatively:
if( jQuery('#elem').get(0) ) {}
if you used:
jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }
you would imply that chaining was possible when it is not.
This would be better
jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }
no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length
if ( $('#someDiv').length ){
}
I prefer the
if (jQuery("#anyElement").is("*")){...}
Which basically checks if this elements is a kind of "*" (any element). Just a cleaner syntax and the "is" makes more sense inside an "if"
For me .exists
doesn't work, so I use the index :
if ($("#elem").index() ! = -1) {}
if you used:
jQuery.fn.exists = function(){return ($(this).length > 0);}
if ($(selector).exists()) { }
you would imply that chaining was possible when it is not.
This would be better
jQuery.exists = function(selector) {return ($(selector).length > 0);}
if ($.exists(selector)) { }
if ($('#elem')[0]) {
// do stuff
}
no, jquery always returns a jquery object regardless if a selector was matched or not. You need to use .length
if ( $('#someDiv').length ){
}
if ($('#elem')[0]) {
// do stuff
}
I think most of the people replying here didn't quite understand the question, or else I might be mistaken.
The question is "how to check whether or not a selector exists in jQuery."
Most people have taken this for "how to check whether an element exists in the DOM using jQuery." Hardly interchangeable.
jQuery allows you to create custom selectors, but see here what happens when you try to use on e before initializing it;
$(':YEAH');
"Syntax error, unrecognized expression: YEAH"
After running into this, I realized it was simply a matter of checking
if ($.expr[':']['YEAH']) {
// Query for your :YEAH selector with ease of mind.
}
Cheers.
Source: Stackoverflow.com