When we offer a text area it may be the case that we have a limit of the characters to be entered. In this case, it would be good to offer the user the number of characters that have been inserted, and in passing the words.
The first thing we are going to do is create a text area in HTML using the TEXTAREA element.
Inside the form, we include a button, which when pressed will invoke the wordCount () method, which contains all the code of the example to count characters and words in JavaScript.
Inside the wordCount method, the first thing to do will be to get the text from the text area (save redundancy). We will do this through the getElementById method.
textArea = document.getElementById ("area"). value;
Once we have the text, we obtain the number of characters through its length (length property).
numberCharacters = text.length;
The next thing is to count the words that the text has. In order to do this, we will use the split function which TRACES a string into substrings taking as a cut character the one we pass as a parameter, and leaving the result in an array of words.
DividedAreaText = TextArea.split ("");
numberWords = textAreaDivided.length;
Top comments (0)