How about this?
$(window).load(function () { $('#submission1, #submission2, #submission3, #submission4').keyup(function() { var len = $(this).val().length; if (len > 1500) { $(this).val($(this).val().substring(0, 1500)); } $('#countCharacters' + $(this).attr('id').slice(-1)).text(1500 - len); }).keyup(); });
This can be simplified even if you have a common class in your text areas, and we can avoid manipulating identifiers at the end, you will give us a little more information about the HTML layout - in particular, how the #countCharacters elements are located in the DOM relative to text fields.
An ideal solution would look something like this:
$(window).load(function () { $('.editors').keyup(function() { var len = $(this).val().length; if (len > 1500) { $(this).val($(this).val().substring(0, 1500)); } $(this).next('.count').text(1500 - len); }).keyup(); });
This requires that your text fields have the editors class, and the count elements have the class count and are located immediately after their respective text areas. A similar solution could be developed regardless of where the elements of the account are actually located.
Update
Based on this HTML that you included in the comment:
<div class="field"> <textarea name="submission1" class="editors" id="submission1" style="width: 680px"><?=$writtenSubmission1;?></textarea> </div> <p align="right"> <span id="countCharacters1" class="count" style="font-weight:bold">1500</span> characters left </p>
The string text() should change to the following:
$(this).closest('.field').next().find('.count').text(1500 - len);
Basically, you just move the DOM tree to get the correct .count element from the textarea $(this) element. See jQuery docs for more information on the features that are available to help you navigate the DOM.
I also made one more minor change above, to run the keyup() function immediately after attaching the event handler to it, to show the correct amount immediately after loading the page.
In this script you can see the working version with your HTML: http://jsfiddle.net/tXArh/