Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to create word count for my webpage, I'm struggling to get it to work. Getting error, pasted below

The error I am getting is index.html:145 Uncaught ReferenceError: countWord is not defined at HTMLTextAreaElement.onkeyup

Adding my code snippet below. I am new to all of this still, js to be specific.

        <form class="contact-form" onsubmit="sendEmail(); reset(); return false;">
            <h3>Get In Touch!</h3>
            <input type="text" id="name" placeholder="Name *" required>
            
            <input type="email" id="email" placeholder="Email *" required>
            
            <input type="number" id="phone" placeholder="Phone Number (Optional)" >
            
            <textarea id="txt" onkeyup="countWord()" onpaste="countWord()" rows="4" placeholder="Message..."></textarea>

            <div class="row mt-3">
                <div class="col">
                    Word Count: <span id="wordCount">0</span>
                </div>
                
            </div>
            
            <button id="clearBtn" type="submit">Send message</button>
            
        </form>
    </section>

const textField = document.getElementById("txt");
const wordCount = document.getElementById("wordCount");
const clearBtn = document.getElementById("clearBtn");

function countWord(){
  let text = textField.value;
  text=text.trim();
  let words = text.split(" ");
  wordCount.innerText = words.length;
} ``` 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You are missing tags. It should look like

<script>
function countWord(){
let text = textField.value;
text=text.trim();
let words = text.split(" ");
wordCount.innerText = words.length;
  }
 </script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading