Reputation: 79
I was figuring some things out for fun. I wanted to create a page to count how many words there exist a textarea. When the user clicks on the button 'Count' it shows below how many words exist. I tried some things but I got no result on my html page.
var form = document.querySelector("form");
var counted = document.querySelector(".counted");
form.addEventListener("count", function(ev) {
ev.preventDefault();
countWords();
}, false);
// var countWords = string => string.split(' ').length;
// countWords(textarea.value);
// document.querySelector('#counted').value = countWords(string);
// function countWords(){
// var textarea = document.querySelector("textarea").value;
//
// document.querySelector("#textarea").value = textarea.split(' ').length;
// document.querySelector(".counted").innerHTML += "There are " + textarea + " words in this text.";
// }
function countWords() {;
string = document.querySelector("#textarea").value;
string = string.replace(/(^\s*)|(\s*$)/gi, "");
string = string.replace(/[ ]{2,}/gi, " ");
string = string.replace(/\n /, "\n");
document.querySelector("#counted").value = string.split(' ').length;
}
<form action="#">
<textarea id="textarea" name="textarea" cols="30" rows="10"></textarea><br><br>
Count how many words<br><br>
<button id="count" name="count">Count</button><br><br>
<p id="counted" class="counted"></p>
</form>
Upvotes: 2
Views: 82
Reputation: 24
Define button type= button.
Count
and here is the onclick function:
function countWords(){;
string = document.querySelector("#textarea").value;
string = string.replace(/(^\s*)|(\s*$)/gi,"");
string = string.replace(/[ ]{2,}/gi," ");
string = string.replace(/\n /,"\n");
var count=document.querySelector("#counted").value;
count= string.split(' ').length;
alert(count);
}
Upvotes: 0
Reputation: 2491
window.onload = (function () {
var count = document.querySelector("#count");
var counted = document.querySelector(".counted");
form.addEventListener("click", function(ev) {
ev.preventDefault();
countWords();
}, false);
function countWords() {
var string = document.querySelector("#textarea").value;
string = string.replace(/(^\s*)|(\s*$)/gi, "");
string = string.replace(/[ ]{2,}/gi, " ");
string = string.replace(/\n /, "\n");
console.log(string);
document.querySelector("#counted").innerHTML = string.split(' ').length;
}
})();
<div id="form">
<textarea id="textarea" name="textarea" cols="30" rows="10"></textarea><br><br>
Count how many words<br><br>
<button id="count" name="count">Count</button><br><br>
<p id="counted" class="counted"></p>
</div>
Use window.onload
so that the event handler registration is done after the DOM is ready. Use innerHTML
to update the text. You don't have to user form
element. Use div
for grouping.
Upvotes: 0
Reputation: 18619
You should...
onSubmit
event instead of onCount
(which isn't valid), and .innerText
or .innerHTML
of an HTML element instead of .value
if it isn't a <input>
or <textarea>
:var form = document.querySelector("form");
var counted = document.querySelector(".counted");
form.addEventListener("submit", function(ev) {
ev.preventDefault();
countWords();
}, false);
// var countWords = string => string.split(' ').length;
// countWords(textarea.value);
// document.querySelector('#counted').value = countWords(string);
// function countWords(){
// var textarea = document.querySelector("textarea").value;
//
// document.querySelector("#textarea").value = textarea.split(' ').length;
// document.querySelector(".counted").innerHTML += "There are " + textarea + " words in this text.";
// }
function countWords() {;
string = document.querySelector("#textarea").value;
string = string.replace(/(^\s*)|(\s*$)/gi, "");
string = string.replace(/[ ]{2,}/gi, " ");
string = string.replace(/\n /, "\n");
document.querySelector("#counted").innerText = string.split(' ').length;
}
<form action="#">
<textarea id="textarea" name="textarea" cols="30" rows="10"></textarea><br><br>
Count how many words<br><br>
<button id="count" name="count">Count</button><br><br>
<p id="counted" class="counted"></p>
</form>
Upvotes: 1