1

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?

4
  • 2
    You may want to invest into using textContent over innerHTML, simply because innerHTML can return nested tags, i.e. div.innerHTML will, or can contain <p>text</p>, etc, you get the idea... Also, with the else clause, you may want to return -1? ... Or alternatively, return textArr.indexOf(main1.textContent). Commented Dec 30, 2018 at 16:05
  • 5
    Your code "does nothing" because innerHTML not innerHtml. Also there is a builtin method for that Array.prototype.indexOf (which even provides more consistent output returning -1 when element is not in array) Commented Dec 30, 2018 at 16:09
  • @YuryTarabanko - This is the correct answer. I can't count the number of times I looked for case mismatches. Thank you Commented Dec 30, 2018 at 16:16
  • 1
    @YuryTarabanko I'm glad you picked up on that, I totally missed that! Commented Dec 30, 2018 at 16:16

5 Answers 5

1

This does the job, as I've said within the comments, using ```innerHtml`` can be a bad idea if you only want the text content of the relevant HTML element. If you want the nested tags also, then sure, it shouldn't be a problem at all, just be careful which one you use, there are times where you will want nested HTML elements returned as a part of the string, other times you won't, etc.

const textArr = ["a", "b", "c"];
const el = document.getElementById("main1");
const getIndex = () => textArr.indexOf(el.textContent);

console.log(getIndex());
<p id="main1">b</p>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, very helpful. I'm really bad at asking questions... I actually have the tags included in the text array because I couldn't figure out how to just get the text
@froggomad You're very welcome! :) .. I actually remember when I was learning, way back when, for some time I was a little unsure as to how to get the text with no HTML tags in it, turns out it was actually this simple, haha.
1

Just use Array.prototype.indexOf() like so:

return textArr.indexOf(main1.textContent);

This will return the index of the matching text from the array and the main1 element.

Comments

1

You were almost there.

var main = document.getElementById("main1").innerHTML;
var textArr = ["a", "b", "c"]


function check(value, array) {

  for (i = 0; i < textArr.length; i++) {
    if (value == array[i]) {
         return value + " is equal to " + array[i]
  } 
 }
}

document.write(check(main, textArr))
document.write("</br>" + check("a", textArr))
document.write("</br>" + check("h", textArr))
<p id="main1" style="display: none">b</p>

Comments

1

Try this Array.prototype.find():

var found = textArr.find(function(element) { 
  return main1.textContent == element;
});

it will return the content of matching element.

2 Comments

Would this be more efficient than using indexOf though? I mean in such a small scenario, sure it probably makes 0 difference with regards to performance, I'm actually not 100% sure what would be more efficient.
It will return undefined and the indexOf() will return -1 if there is no matching element, anyway I don't think the performance is important in that situation, think about which one is more comfortable for you.
0

main1 isn't defined. I would just set that innerHTML to a variable and then compare.

var arr = ["a", "b", "c", "d"];
var answer = document.getElementById("main1").innerHTML;
var index;
for (index = 0; index < arr.length; index++) {
    if (answer == arr[index]) {
        return index;
    } 
}

1 Comment

main1 actually is defined in my code and I forgot to include it here, but regardless doesn't need to be explicitly defined as id's are automatically globally accessible as was pointed out in another answer by someone smarter than me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.