-2

I want to make a word count without any input tag. I want to use just p tag and span tag. How can I make it?

<span class="word" id="word">Words Number</span> <p class="any" id="any">Here have some text which I want to count</p>
  • Does this answer your question? [Counting words in string](https://stackoverflow.com/questions/18679576/counting-words-in-string) – Archit Gargi May 30 '22 at 11:25

3 Answers3

1

First, you need to get value of p

let countVal = document.getElementById('wordCount');

Then, make an array from it by using split(separator)

let countArr = countVal.split(" ")

Finally, display it in your js

document.getElementById('someId').textContent = countArr.length;
Azamat
  • 329
  • 1
  • 9
0

You can use these codes:

For JS: document.querySelectorAll('#word')[0].textContent.length

For Jquery:

$("#word").text().length; 
$("#any").text().length; 
0

you have same value for class and id in html , change it if possible like this

<span class="Word" id="word">Words Number</span> <p class="Any" id="any">Here have some text which I want to count</p>

Then use this jQuery code

$(document).ready(function(){

var myString = $("#any").text();

var countWord = myString.split(' ').length;

});

Satish Thakur
  • 52
  • 2
  • 10