character limit/countdown error

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

character limit/countdown error

Post 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>
dgreenhouse
Forum Newbie
Posts: 20
Joined: Tue Mar 10, 2009 5:13 am

Re: character limit/countdown error

Post 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>
Post Reply