if ($('#myElement').length > 0) {
// it exists
}
Or to make it a fancy function with a callback:
// Tiny jQuery Plugin
// by Chris Goodchild
$.fn.exists = function(callback) {
var args = [].slice.call(arguments, 1);
if (this.length) {
callback.call(this, args);
}
return this;
};
// Usage
$('div.test').exists(function() {
this.append('<p>I exist!</p>');
});
You don’t need the “> 0”, you can simply write:
if ( $('#myElement').length ) {
// it exists
}
No true, since jQuery always returns an object with length 0
it’s true. If element exists: length > 0, and condition is true.
If element not exosts: length = 0 and condition is false
This IS correct. You do NOT need to say length > 0, because 1 = true, 0 = false.
Eliazer, you probably just heard someone say “JQuery always returns an object” and thus you think that every FUNCTION of jQuery returns an object. jQuerys DEFAULT selector “$()” always does. However its functions don’t always… ie “has” and “is”… or “length”
Length returns 0 or 1, and since 1 = true, 0 = false.
pretty good.
Thank you Nathan.
Even easyest and reusable, you can define a function:
jQuery.fn.exists = function() { return this.length>0; };
And use it, anywhere, simple like this:
if ($(‘#myElemento).existe()) { … }
Ummm, I think I see a spelling mistake in the last line of your comment:
if ($(‘#myElemento).existe()) { … }
is suppose to be
if ($(‘#myElemento).exists()) { … }
(existe compared to exists)
Better,
if($(element).size()){
// …
}
That’s not better at all.
jQuery#size
simply returns thelength
property, so just save the function call and access thelength
property directly.nice tuto :P
But why you don’t use
$('#myElement')[0]
? – returns the first DOM element orundefined
, I think.I use this:
if($(“element”)[0] || $(“element”)[0])
{
// do something
}
p.s. Exemple for multiple elements
i think “!=” is more performant than “>”, what do you think about?
if ( $(‘#myElement’).length != 0 ) {
// it exists
}
0 means ‘false’
>= 1 means ‘true’
if( $(‘#selector’).length )
{
//exec,if length >= 1
}
else
{
//exec,if length = 0
}
Hi folks, that checking is nice, but I am getting crazy looking how could I trigger that function when a element is created within the DOM context. Basicly I want stop #something.cycle when an iframe appears on my document (from js live editor)….
Jordi,
Did you very figure out how to trigger a function when an element is created? I am running into this issue myself, and not finding much that would help.
Thank you,
-N8
This may be of some help:
http://stackoverflow.com/questions/14667770/how-to-call-a-javascript-function-when-iframe-finished-loading
http://stackoverflow.com/questions/6386128/how-can-i-call-a-function-after-an-element-has-been-created-in-jquery
Guys, I have used this and does not work
it is not detected
if($(‘#body’).length) bla bla
of course if I use and id body in body tag works, but if it does not have the id=’body’ this detector does not work at all
always means that your matching the id. you should use $(‘body’) instead of $(‘#body’)
Is this function working with “live” or “bind”?
I haven’t checked but it should since it’s returning ‘this’;
live esdck
Hey, this code doesn’t work
$(‘#stop’).bind(‘click’, function(){
stopMove();
jQuery.fn.exists = function(){return this.length>0;}
Need a little help. I have used it approx 1000 times but still can not get it right this time:
My if statement looks like this:
if($(‘#s’+user_id + ‘ .course_stats_user’).length > 0){
}
‘#s’+user_id + ‘ .course_stats_user’ does not exist but still it goes in the if loop…any ideas..
NVM.. it is now resolved..
I spent so long futzing with this… Leave it to CSS-Tricks to distribute valuable knowledge. Thank you!
Using .exists() seems the logical choice over .length, no?
I see a lot of way in discussion to check ID of div using jquery.
I tried test and all of them is correct.
I using if( $(‘#myElement’).length) to quick in use.
Thank for nice post.
A useful post, especially the
exists()
function. Not explicitly mentioned above, but worth stating, theexists()
method is chainable.A slight tweak/safety check to that would be to check that the first argument passed through is actually a function: