This question already has an answer here:
How do I check if an element exists if the element is created by .append()
method?
$('elemId').length
doesn't work for me.
$('elemId').length
doesn't work for me.
You need to put #
before element id:
$('#elemId').length
---^
With vanilla JavaScript, you don't need the hash (#
) e.g. document.getElementById('id_here')
, however when using jQuery, you do need to put hash to target elements based on id
just like CSS.
document.querySelector('#id_here')
requires the hash sign just as much as the jQuery equivalent $('#id_here')
. - AndreKR
Try to check the length of the selector, if it returns you something then the element must exists else not.
if( $('#selector').length ) // use this if you are using id to check
{
// it exists
}
if( $('.selector').length ) // use this if you are using class to check
{
// it exists
}
function exists(elementName) { return $(elementName).length; }
- Sinister Beard
Try this:
if ($("#mydiv").length > 0){
// do something here
}
The length property will return zero if element does not exists.
> 0
. Any ideas why that might happen? - KillahB
How do I check if an element exists
if ($("#mydiv").length){ }
If it is 0
, it will evaluate to false
, anything more than that true
.
There is no need for a greater than, less than comparison.
your elemId
as its name suggests, is an Id
attribute, these are all you can do to check if it exists:
Vanilla JavaScript: in case you have more advanced selectors:
//you can use it for more advanced selectors
if(document.querySelectorAll("#elemId").length){}
if(document.querySelector("#elemId")){}
//you can use it if your selector has only an Id attribute
if(document.getElementById("elemId")){}
jQuery:
if(jQuery("#elemId").length){}
!= null
as querySelector
will always return null
(which is falsey) or an element - 1j01if (document.querySelector("elemId") != null )
just to improve code readability - Mrinmoyif (document.querySelector("elemId") != null )
has no effect in writing a more readable code. In JavaScript world instead of keeping your mindset from structural programming languages like Java or C# it is highly recommended to get to know the conventions of JavaScript community. For a JavaScript developer in this specific case != null
is totally redundant and much less readable. - Mehran HatamiquerySelector()
returns null or 0
or any of the other 5 values. querySelectorAll()
and jQuery has a different return value when it dont find an element. Please consider that people these days work on polyglot of techs and frameworks so making things more readable is sure to benefit. By the way I am more a javascripter than a java/golang programmer - Mrinmoy
You can also use array-like notation and check for the first element.
The first element of an empty array or collection is simply undefined
, so you get the "normal" javascript truthy/falsy behaviour:
var el = $('body')[0];
if (el) {
console.log('element found', el);
}
if (!el) {
console.log('no element found');
}
You can use native JS to test for the existence of an object:
if (document.getElementById('elemId') instanceof Object){
// do something here
}
Don't forget, jQuery is nothing more than a sophisticated (and very useful) wrapper around native Javascript commands and properties
instanceof Object
- 1j01
If you have a class on your element, then you can try the following:
if( $('.exists_content').hasClass('exists_content') ){
//element available
}
.length
works just fine, see here: jsfiddle.net/yahavbr/A9zW2 if you did use#
post your code and we'll see what you done wrong. - Shadow Wizard