javascript - How do I get a form to stop after a curtain amount of charackters? -
javascript - How do I get a form to stop after a curtain amount of charackters? -
so got code, cant figure out how create stop after curtain amout of charackers, how can that? in advance answers!.
class="snippet-code-html lang-html prettyprint-override"><!doctype html> <html> <head> <title>hover</title> <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery- 1.8.1.min.js"> </script> </head> <body> <h1>keyup</h1> <hr> <form action="#"> <textarea rows="10" cols="20" id="message" name="message"> </textarea> <p>characters remaining: <span id="remaining">50</span></p> </form> <script type="text/javascript"> $(document).ready(function(){ var maxcharacters = 50; $("#message").on("keyup", function(){ var currentval = $("#message").val().length; var totalremaining = maxcharacters - currentval; $("#remaining").text(totalremaining); }); }); </script> </body> </html>
try demo
$(document).ready(function(){ var maxcharacters = 50; $("#message").on("keyup keydown", function(event){ var currentval = $("#message").val().length; var totalremaining = maxcharacters - currentval; if(+$(this).val().length >= maxcharacters) event.preventdefault(); // stop default action of event $("#remaining").text(totalremaining); }); });
hope helps ...
javascript
Comments
Post a Comment