Skip to content

Instantly share code, notes, and snippets.

@timothy-okoduwa
Created October 14, 2022 12:46
Show Gist options
  • Save timothy-okoduwa/0f40d8b788ac6bbc3236323e0c111af5 to your computer and use it in GitHub Desktop.
Save timothy-okoduwa/0f40d8b788ac6bbc3236323e0c111af5 to your computer and use it in GitHub Desktop.
set a max value for textarea
<!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