Page 1 of 1

character limit/countdown error

Posted: Thu Mar 17, 2011 9:37 pm
by someguyhere
I'm using the following code on forms on two sites, it works fine on one, on the other, it doesn't. The only difference is the name of the field (it's company_desc on one site and company_description on the other)

Can anyone see anything that I may be missing here?

Code: Select all

<textarea name="company_description" cols="48" rows="6" onkeydown="limitText(this.form.company_description,this.form.countdown,600);" onkeyup="limitText(this.form.company_description,this.form.countdown,600);"></textarea><br>
<p><i>(Maximum characters: 600) - You have <input readonly="readonly" name="countdown" size="3" value="600" type="text"> characters remaining.</i></p>

Re: character limit/countdown error

Posted: Sun Mar 27, 2011 7:01 am
by dgreenhouse
This should work with jQuery:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Use jQuery to limit chars entered</title>
<style>
  #message {color:blue;}
</style>
<script type="text/javascript" src="../includes/jquery/jquery-1.5.1.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $('#message').text('You have ' + $('#myelem').attr('maxlength') + ' characters left.');
		
    $('#myelem').bind('keyup', function(event) {
      var maxchars = parseInt($('#myelem').attr('maxlength'));
			var chars = $('#myelem').val().length;
      $('#message').text('You have ' + (maxchars-chars) + ' characters left.');
    });
  });
</script>
</head>
<body>
  <textarea id="myelem" maxlength="60" rows="10" cols="50"></textarea>
  <p id="message"></p>
</body>
</html>