I am not very good with javascript but I have kludged some javascript togethter to make a function that works well, but I need to make it more general by being able to pass it the form elements so that I don't have to write ten seperate functions of the same code. I think that there is a way to pass the form objects but am having a difficult time making it work. Here is what I have :
this code essentially counts down the number of characters that is in a textarea
Code:
<script language="JavaScript">
<!--
function ChangeCharacter() {
MAX_LENGTH = 2000;
document.frmStep3.txtRemaining.value= 2000 - document.frmStep3.txtLocationDirections.lenght;
if ( ( document.frmStep3.txtLocationDirections.value == "" ) || ( document.frmStep3.txtLocationDirections.value.length == 0 ) ) {
remainingCharacters = MAX_LENGTH;
document.frmStep3.txtRemaining.value = remainingCharacters;
} else {
remainingCharacters = MAX_LENGTH - (document.frmStep3.txtLocationDirections.value.length);
document.frmStep3.txtRemaining.value = remainingCharacters;
}
if ( document.frmStep3.txtLocationDirections.value < (MAX_LENGTH + 1) ) {
document.frmStep3.txtRemaining.value = remainingCharacters;
}
if ( ( document.frmStep3.txtRemaining.value < 0 ) ) {
document.frmStep3.txtLocationDirections.value = document.frmStep3.txtLocationDirections.value.substring(0,MAX_LENGTH);
document.frmStep3.txtRemaining.value = 0;
alert("Maximum message length reached. Your message has been truncated at " + MAX_LENGTH + " characters.");
}
}
//-->
</script>
then in the HTML
<textarea name="txtLocationDirections" cols="60" rows="5" wrap="virtual" onKeyUp="ChangeCharacter()" onKeyDown="ChangeCharacter()" onBlur ="ChangeCharacter"></textarea>
<input type="text" name="txtRemaining" maxlength="4" value="2000" size="4">
this code essentially counts down the number of characters that is in a textarea