Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change div value from countdown text box 1

Status
Not open for further replies.

onressy

Programmer
Mar 7, 2006
421
CA
Hi, i'm trying to change a div tag value to the new countdown value. The count down is for a text box only allowing 50 characters, can't seem to get the elementByID correct, this is what i have:
//Thanks for reading this
Code:
<head>

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);

	} else {
		document.getElementById("countdown").innerHTML = limitNum - limitField.value.length;

	}
}</script>

</head>.....<body>

<input name='limitedtextfield' type='text' onKeyDown='limitText(this.form.limitedtextfield,this.form.countdown,50);' onKeyUp='limitText(this.form.limitedtextfield,getElementById("countdown").innerHTML,50);' maxlength='50'>


(Maximum characters: 50<div id='countdown'>50</div>)
 
The script seems unnecessary, if you are limiting the text field then you are by default also limiting the div, just try this:
Code:
<script type="text/javascript">
function limitText(str) {
   document.getElementById("countdown").innerHTML = str;
}
</script>
<input name='limitedtextfield' type='text' onKeyUp='limitText(this.value);' maxlength='50'>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
But there is a difference between the two script, your script displays via div the characters typed into the text box, my script (although it doesn't work) its intention was to countdown starting at 50, the allowable characters left for the text box input.
 
Sorry, I wasn't quite sure what the function was supposed to do so I guessed. Make the following changes:
Code:
<script type="text/javascript">
function limitText(str[!], limit[/!]) {
   document.getElementById("countdown").innerHTML = [!]limit - str.length[/!];
}
</script>
<input name='limitedtextfield' type='text' onKeyUp='limitText(this.value, [!]parseInt(this.maxlength, 10)[/!]);' maxlength='50'>

That should work, if it doesn't like passing in this.maxlength from the function call then you can always just hard-code in the number 50.

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top