Dynamic HTML element functionThe code in here with a little bit of modification and simplification:
<input disabled maxlength="3" size="3" value="10" id="counter">
<textarea onkeyup="textCounter(this,'counter',10);" id="message">
</textarea>
<script>
function textCounter(field,field2,maxlimit)
{
var countfield = document.getElementById(field2);
if ( field.value.length > maxlimit ) {
field.value = field.value.substring( 0, maxlimit );
return false;
} else {
countfield.value = maxlimit - field.value.length;
}
}
</script>
Hope this helps!
tip:
When merging the codes with your page, make sure the HTML elements(textarea
, input
) are loaded first before the scripts (Javascript functions)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…