- Here is a sample JavaScript code that you can use to display the word count of a text in a div element, and to update the word count dynamically when the text in the textbox changes:
- // Get the div element
- var div = document.getElementById("wordcount");
- // Get the textbox element
- var textbox = document.getElementById("textbox");
- // Create an event listener that will update the word count when the text in the textbox changes
- textbox.addEventListener("input", function() {
- // Get the word count of the text in the textbox
- var wordCount = textbox.value.split(" ").length;
- // Display the word count in the div
- div.innerHTML = "Word Count: " + wordCount;
- });
- In this code, the textbox element is assigned an input event listener, which will be called every time the text in the textbox changes. When the event listener is called, the word count of the text in the textbox is calculated, and it is displayed in the div element by setting the innerHTML property of the div element to the word count.
- You can customize this code to suit your specific needs, such as changing the text that is displayed in the div element, or using a different element to display the word count.