mohsin shah
mohsin shah

Reputation: 13

what is object html collection in javascript

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

Answers (3)

Nachiketha
Nachiketha

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

Muhammad Faran Ali
Muhammad Faran Ali

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

Deepak Biswal
Deepak Biswal

Reputation: 4320

Try this:

    var allPTags = document.getElementsByTagName("p");
    var allPTagsCount = allPTags.length;
    if(allPTags !== "") { 
       alert(something);
    }

Upvotes: 1

Related Questions