Reputation: 6079
I would like to test if an element exists but i cannot do it with $('#id')length > 0
because i am testing an input element which might not contain any characters and then it results in 0 even if it exists...
How can i test if this element exists??
Upvotes: 0
Views: 1422
Reputation: 148110
Do it like this, Demo on JsFiddle
With jQuery
$('#id').length > 0
With Javascript
if(document.getElementById('ID') != null)
{
alert("Element found");
}
Upvotes: 1
Reputation: 218722
length
property is not returning the characters in the element. but the number of elements in the jQuery object. So you should be good to use length property in your case.
Upvotes: 2