How do I return the sum total of the words repeated, within all div?

2

To illustrate the degree of difficulty, I put 3 div and within them identical words, put it on purpose to add to its duplication.

<div>        <div>        <div>
   Diego       Ademir      Diego
   Maicon      Diego       Maicon
   Adriana     Maicon      Ademir
   Magda       Adriana     Adriana
   Delvair     Magda       Ademir
   Roselene    Delvair     Delvair
   Lawiny      Ademir      Lawiny
   Nicolas     Lawiny      Roselene
   Alice       Nicolas     Nicolas
   Ademir      Alice       Alice
   Júlia       Júlia       Ademir
</div>       </div>       </div>

I want to go through 3% with% with% with%, and returns the total number of repetition of the name "Ademir" in div . Here's an example with for :

var y = ["Diego","Maicon","Ademir","Adriana","Ademir","Delvair","Lawiny","Roselene","Nicolas","Alice","Ademir"];

var objects= {};

for (var x in y) {

objects[y[x]] = objects[y[x]]!=undefined ? objects[y[x]]+1 : 1;
}

alert("Encontramos " + objects.Ademir + " repetição do nome 'Ademir'!");

Based on the given example, I want to do with alert and add each equal word found within 3 array , and return its total.

Remembering that for the word in which it should be searched / added it is called "Ademir".

    
asked by anonymous 23.03.2017 / 18:29

1 answer

1

Just select the whole text and count the occurrences of the word in string , see example here .

I created a div to hold the others, so I took the selected text from it and counted the issues.

//Pega texto
var str = document.getElementById("buscaraqui").textContent;
//conta o texto
var count = (str.match(/Ademir/g) || []).length;
document.getElementById("res").textContent = count
   <div id='buscaraqui'>

   <div>        <div>        <div>
  Diego       Ademir      Diego
  Maicon      Diego       Maicon
  Adriana     Maicon      Ademir
  Magda       Adriana     Adriana
  Delvair     Magda       Ademir
  Roselene    Delvair     Delvair
  Lawiny      Ademir      Lawiny
  Nicolas     Lawiny      Roselene
  Alice       Nicolas     Nicolas
  Ademir      Alice       Alice
  Júlia       Júlia       Ademir
   </div>       </div>       </div>

   </div>
   <p>O resultado é: <span id='res'></span></p>

Other References

23.03.2017 / 18:52