I would like to count occurency of specific word/pharse in a HTML body. For example in a paragraph. The special thing is that i want to count by matching with a variable - that refers to string, not a string itself.
The code below returns only zeroes.
Maybe it's something with array_values --> like it doesn't see it as an array. I'm a begginer so every hint matters.
var array_values = document.getElementsByClassName('a'); // <p class="a"> A paragraph that contains some text.
var chosenWord = document.getElementById("input").value; // a word that i would like to count (how many times it occours in paragraph)
var count = 0;
for (var i = 0; i < array_values.length; i++) {
if (array_values[i] == chosenWord)
count++;
}
alert("The word " + chosenWord + "occours " + count + "times");