Possible Duplicate:
Stop submit button being pressed until enough words are written in text area
So basically I have the code below. I wish to enable the submit button when the word count in the text area reaches more than 5. The code below isn’t working. Ideas?
<form method="post" id="writearticle" action="submitarticle.php">
<textarea rows="30" cols="85" id="content" name="content" placeholder="Write the article here. Try to abide by the instructions and keywords provided." onkeydown="checkWordCount();" ></textarea>
<br />
<input type="submit" id="submit" name="submitarticle" value="Submit Article" class="submit" disabled />
<script type="text/javascript">
function checkWordCount() {
var wordCount = document.getElementById('content').value.split(" ").length;
if (wordCount > 5) {
document.getElementById('submit').disabled = false;
} else {
document.getElementById('submit').disabled = true; alert("lol");
}
}
</script>
</form>
Your code is fine, the problem is the event that you’re firing on. If you enter more than 5 words in the
textarea, thesubmitbutton is enabled, but then if you highlight all of the text, and remove it, thewordCountremains the same, so thesubmitbutton isn’t disabled.Try changing
onkeydowntoonkeyup, that should fix the problem.edit
In response to your comments, check that the IDs of the elements you are using are unique, if not, it will cause problems!