Created
October 14, 2022 12:46
-
-
Save timothy-okoduwa/0f40d8b788ac6bbc3236323e0c111af5 to your computer and use it in GitHub Desktop.
set a max value for textarea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>How to set maxlength for textarea in javascript?</title> | |
</head> | |
<body> | |
<div > | |
<form> | |
<textarea name="message" placeholder="Write Here..." onkeydown="limitTextOnKeyUpDown(this.form.message,this.form.countdown,160);" onkeyup='limitTextOnKeyUpDown(this.form.message,this.form.countdown,160);'></textarea> | |
You have | |
<input readonly type="text" name="countdown" size="3" value="160"> chars are left | |
</form> | |
</div> | |
<script type="text/javascript"> | |
function limitTextOnKeyUpDown(limitField, limitCount, limitNum) { | |
if (limitField.value.length > limitNum) { | |
limitField.value = limitField.value.substring(0, limitNum); | |
} else { | |
limitCount.value = limitNum - limitField.value.length; | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment