简体   繁体   中英

Checking innerHTML values if empty or not for Array of Object

I am trying to check if below condition holds true:

var elemArray = document.getElementsByClassName('customers');

    for(var i = 0; i < elemArray.length; i++){
       elemArray[i].id = i;
       var elem = document.getElementById(elemArray[i].id);
       var text = elem.innerHTML;
       text = text.toString();
       alert(text);
       if(text=="<br>"){
            alert('bingo');
            elem.style.visibility="hidden";
         }

     }      

In the 1st alert I can see values as

 1. <br>ABC
 2. <br>XYZ 

and for empty values I get:

1. <br>
2. <br>

so I am trying to compare it but "bingo" is not showing. What is it that I am missing?

Looks like you have either special characters or spaces in the content. Remember == compares exact strings. ie '<br>' and '<br> ' are different.

Here is a working jsfiddle for and it prints bingo just fine. http://jsfiddle.net/1nxzxjdd/

Some answers mention using indexOf or contains method which might not be useful to OPs question because it looks like OP is interested in finding out values containing only <br>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM