How to count times of appearance of a certain word in a div full of text in JavaScript?

ZY Cao

I am trying to create an event function that's associated with a button that, when clicked after you input a phrase into the text area, counts how many times this phrase has appeared in the text (only the p elements) and simultaneously output the value (how many times) into a span element that this function creates.

The intended result is this: enter image description here

This is my html:

<body>
<div id="main">
<p>The Phoenix Suns are a professional basketball team based in Phoenix, 
    Arizona. They are members of the ...</p>
<p>The Suns have been generally successful since they began play as an
 expansion team in 1968. In forty years of play they have posted ...</p>
<p>On January 22, 1968, the NBA awarded expansion franchises to an ownership 
group from Phoenix and one from Milwaukee. ...</p>
<ul>
    <li>Richard L. Bloch, investment broker/real estate developer...</li> 
    <li>Karl Eller, outdoor advertising company owner and former...</li>
    <li>Donald Pitt, Tucson-based attorney;</li>
    <li>Don Diamond, Tucson-based real estate investor.</li>
</ul>
</div>

<p>
Page by Marty Stepp. <br />
Some (all) information taken from Wikipedia.
</p>
<hr />

<div>
Search for text:
<input id="searchtext" type="text"  /> 
<button id="searchbutton">Search</button>
</div>
</body>

And this is my js function:

function count_search(event){
let span = document.createElement('span');
span.setAttribute("id", "output");
document.body.appendChild(span);
var searchPhrase = document.querySelector("#searchtext").value;
var main = document.querySelector("#main");
var mainParas = main.querySelectorAll(" p ").textContent;
document.getElementById("#output").innerHTML = 
          (mainParas.match(/searchPhrase/g)).length;
/*var times = (mainParas.match(/searchPhrase/g) || []).length;
var content = "Searched for the word '" + searchPhrase + "' for " + times + " 
times. "; 
document.getElementById("output").innerHTML = content; */
}

However, the result just won't show. Can you please help me? Any suggestion is much appreciated!

urchmaney

<body>
<div id="main">
<p>The Phoenix Suns are a professional basketball team based in Phoenix, 
    Arizona. They are members of the ...</p>
<p>The Suns have been generally successful since they began play as an
 expansion team in 1968. In forty years of play they have posted ...</p>
<p>On January 22, 1968, the NBA awarded expansion franchises to an ownership 
group from Phoenix and one from Milwaukee. ...</p>
<ul>
    <li>Richard L. Bloch, investment broker/real estate developer...</li> 
    <li>Karl Eller, outdoor advertising company owner and former...</li>
    <li>Donald Pitt, Tucson-based attorney;</li>
    <li>Don Diamond, Tucson-based real estate investor.</li>
</ul>
</div>

<p>
Page by Marty Stepp. <br />
Some (all) information taken from Wikipedia.
</p>
<hr />

<div>
Search for text:
<input id="searchtext" type="text"  /> 
<button id="searchbutton" onClick="count_search()">Search</button>
</div>
<script>
  function count_search(event){
      span = document.createElement('span');
      span.setAttribute("id", "output");
      document.body.appendChild(span);
      
      var searchPhrase = document.querySelector("#searchtext").value;
      searchPhrase = new RegExp(searchPhrase, 'g');
      var main = document.querySelector("#main");
      let count = 0;
      var mainParas = main.querySelectorAll("p").forEach(ele => {
       
        const times = ele.innerHTML.match(searchPhrase);
        count += times ? times.length : 0;
      });
      span.innerHTML = count;
      
  }
</script>
</body>

So the reason it was not showing up before is cause there was no onclick event associated with the button. The following code should help in a way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to count the number of times a word is in the text file

From Dev

Count how many times certain text combinations occurs in certain columns

From Dev

Count how many times a word appears in a text file

From Dev

How to not count certain strings in a text document word counter program

From Dev

How to count occurences of a word in a certain element in a text file?

From Dev

Excel - how to count how many times a word appears within a certain date range

From Dev

Count the ammount of times a word is repeated in a text file

From Dev

How to count how many times a letter appear in a text javascript

From Dev

EXCEL: I want to count how many times a certain word appears in a column if it's in the same row if another word appears

From Dev

How can I count the appearance # of a word (not total appearances)?

From Dev

How to count how many times each individual word appears in text file

From Dev

Count how many times a word exist in column

From Dev

Kibana 7.x how to count full text instead of each word in visualize

From Dev

Count frequency of number appearance in full pandas dataframe

From Dev

HTML and Javascript text area word count

From Dev

How to grab certain word inside contenteditable div

From Dev

How to Count the Items that Occur a Certain Number of Times in a List

From Dev

How to count entries in excel column only certain number of times?

From Dev

Count how many times a certain value per user has changed

From Python

Recursion function to count how many times a certain sequence appears

From Java

Look for a certain String inside another and count how many times it appears

From Dev

Count how many times a column contains a certain value in Pandas

From Dev

How to count how many times a word is in the start and in the end?

From Dev

how to count how many times each word appears?

From Dev

How to find and count a word in a text string?

From Dev

How to count the times a word appears in a file using a shell?

From Dev

Is there a way to count how many times a specific word appears in R

From Dev

How to count number of times a particular word appears in a column?

From Dev

Python: Count how many times a word occurs in a file

Related Related

  1. 1

    How to count the number of times a word is in the text file

  2. 2

    Count how many times certain text combinations occurs in certain columns

  3. 3

    Count how many times a word appears in a text file

  4. 4

    How to not count certain strings in a text document word counter program

  5. 5

    How to count occurences of a word in a certain element in a text file?

  6. 6

    Excel - how to count how many times a word appears within a certain date range

  7. 7

    Count the ammount of times a word is repeated in a text file

  8. 8

    How to count how many times a letter appear in a text javascript

  9. 9

    EXCEL: I want to count how many times a certain word appears in a column if it's in the same row if another word appears

  10. 10

    How can I count the appearance # of a word (not total appearances)?

  11. 11

    How to count how many times each individual word appears in text file

  12. 12

    Count how many times a word exist in column

  13. 13

    Kibana 7.x how to count full text instead of each word in visualize

  14. 14

    Count frequency of number appearance in full pandas dataframe

  15. 15

    HTML and Javascript text area word count

  16. 16

    How to grab certain word inside contenteditable div

  17. 17

    How to Count the Items that Occur a Certain Number of Times in a List

  18. 18

    How to count entries in excel column only certain number of times?

  19. 19

    Count how many times a certain value per user has changed

  20. 20

    Recursion function to count how many times a certain sequence appears

  21. 21

    Look for a certain String inside another and count how many times it appears

  22. 22

    Count how many times a column contains a certain value in Pandas

  23. 23

    How to count how many times a word is in the start and in the end?

  24. 24

    how to count how many times each word appears?

  25. 25

    How to find and count a word in a text string?

  26. 26

    How to count the times a word appears in a file using a shell?

  27. 27

    Is there a way to count how many times a specific word appears in R

  28. 28

    How to count number of times a particular word appears in a column?

  29. 29

    Python: Count how many times a word occurs in a file

HotTag

Archive