I'm comparing what's in a DOM element's innerHTML to what's in a static text array. What's in the element always matches an entry in the text array.
<p id="main1">b</p>
var textArr = ["<p>a</p>","<p>b</p>","<p>c</p>"];
if (main1.innerHTML == textArr[0]) {
return 0;
} else if (main1.innerHTML == textArr[1]) {
return 1;
} else if (main1.innerHTML == textArr[2]) {
return 2;
} else if (main1.innerHTML == textArr[3]) {
return 3;
} else {
return 0;
}
//returns 1
I'll soon be adding entries to textArr and realize a for loop is much better practice in general for such a case anyway. However, my conditional statement is never hit with the loop.
var index;
for (index = 0; index < textArr.length; ++index) {
if (main1.innerHtml == textArr[index]) {
return index;
}
}
//does nothing
How can I check what's in the element against what's in the array?
textContentoverinnerHTML, simply becauseinnerHTMLcan return nested tags, i.e.div.innerHTMLwill, or can contain<p>text</p>, etc, you get the idea... Also, with theelseclause, you may want to return -1? ... Or alternatively, returntextArr.indexOf(main1.textContent).innerHTMLnotinnerHtml. Also there is a builtin method for thatArray.prototype.indexOf(which even provides more consistent output returning -1 when element is not in array)