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>
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>
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;
You can use these codes:
For JS:
document.querySelectorAll('#word')[0].textContent.length
For Jquery:
$("#word").text().length;
$("#any").text().length;
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;
});