Max-Length for multiple text areas using php and Javascript -
i trying display number of characters left in multiple text areas. although have different id's text areas, max length prompted 2nd text area , not first. code shown below
<textarea id="txtbox"></textarea> <input type="text" id="counterbox"/> <script> var txtboxref = document.queryselector("#txtbox"); var counterref = document.queryselector("#counterbox"); txtboxref.addeventlistener("keydown",function(){ var remlength = 0; remlength = 160 - parseint(txtboxref.value.length); if(remlength < 0) { txtboxref.value = txtboxref.value.substring(0, 160); return false; } counterref.value = remlength + " characters remaining..."; },true); </script> <textarea id="txtbox1"></textarea> <input type="text" id="counterbox1"/> <script> var txtboxref = document.queryselector("#txtbox1"); var counterref = document.queryselector("#counterbox1"); txtboxref.addeventlistener("keydown",function(){ var remlength = 0; remlength = 160 - parseint(txtboxref.value.length); if(remlength < 0) { txtboxref.value = txtboxref.value.substring(0, 160); return false; } counterref.value = remlength + " characters remaining..."; },true); </script>
the result below (what have):
what want :
looks variables txtboxref
, counterref
in same scope area, declared twice.
<textarea id="txtbox"></textarea> <input type="text" id="counterbox"/> <script> var txtboxref = document.queryselector("#txtbox"); var counterref = document.queryselector("#counterbox"); txtboxref.addeventlistener("keydown",function(){ var remlength = 0; remlength = 160 - parseint(txtboxref.value.length); if(remlength < 0) { txtboxref.value = txtboxref.value.substring(0, 160); return false; } counterref.value = remlength + " characters remaining..."; },true); </script> <textarea id="txtbox1"></textarea> <input type="text" id="counterbox1"/> <script> var txtboxref1 = document.queryselector("#txtbox1"); var counterref1 = document.queryselector("#counterbox1"); txtboxref1.addeventlistener("keydown",function(){ var remlength = 0; remlength = 160 - parseint(txtboxref1.value.length); if(remlength < 0) { txtboxref1.value = txtboxref1.value.substring(0, 160); return false; } counterref1.value = remlength + " characters remaining..."; },true); </script>
check fiddle
Comments
Post a Comment