Javascript character count

后端 未结 4 1292
渐次进展
渐次进展 2020-12-16 06:00

Example here: http://jsfiddle.net/67XDq/1/

I have the following HTML:


    17.
    

        
4条回答
  •  我在风中等你
    2020-12-16 06:53

    There are two issues in the fiddle

    • no form element
    • script mode was onload, which means that window object didnt have textCounter function

    see updated fiddle http://jsfiddle.net/67XDq/7/, markup:

    
       17.
       
        Questions? Maximum of 500 characters - 
         characters left
        

    and code

    function textCounter(field, cnt, maxlimit) {         
      var cntfield = document.getElementById(cnt)   
      if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
      else
        cntfield.value = maxlimit - field.value.length;
    }
    

提交回复
热议问题