Reputation: 13
If I try to get var allPTags = document.getElementsByTagName("p");
and there is no p tag at all in the document the variable is still not empty there is still something. If I do if(allPTags !== "") { alert(something);}
it dose not alert; How do I compare empty value in it?
Upvotes: 1
Views: 450
Reputation: 23555
Because resultant is an empty array, So you should do the following:
var allPTags = document.getElementsByTagName("p");
if(allPTags.length) {
alert(something);
}
Upvotes: 1
Reputation: 499
Try this:
var allPTags = document.getElementsByTagName("p");
if(allPTags.length>0){
alert("stackoverflow"); // your message if any P tag is present.
}
Upvotes: 1
Reputation: 4320
Try this:
var allPTags = document.getElementsByTagName("p");
var allPTagsCount = allPTags.length;
if(allPTags !== "") {
alert(something);
}
Upvotes: 1