Javascript character count

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

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

I have the following HTML:


    17.
    

        
相关标签:
4条回答
  • 2020-12-16 06:34

    see fiddle: http://jsfiddle.net/abhiklpm/67XDq/15/

    modified the function:

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

    also you were missing id id="q17length" in your html

    edited: also u were not passing the ids as string: textCounter('q17','q17length','500');

    0 讨论(0)
  • 2020-12-16 06:36

    CHange your html to remove all that onkey stuff

    <tr id="rq17">
      <td class='qnum'>17.</td>
      <td class='qtext'>Questions? <i>Maximum of 500 characters - <input id="charsLeft" style="color:red;font-size:12pt;font-style:italic;" readonly type="text" name="q17length" size="3" maxlength="3" value="500"> characters left</i><br/><textarea class="scanwid" name="q17" id="q17" rows="5" cols="" maxlength="500"></textarea></td>
    </tr>
    

    And the javascript is this:

     $("#q17").keyup(function() {
        $('#charsLeft').val(500 - $(this).val().length);
    });
    

    Here's a fiddle: http://jsfiddle.net/67XDq/11/

    0 讨论(0)
  • 2020-12-16 06:38
    var tweet = prompt("Write Tweet:");
    
    var tweetCount = tweet.length;
    
    alert("You have written " + tweetCount + " characters, you have " + (140- tweetCount) + " Chartacters remaining")
    
    0 讨论(0)
  • 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:

    <tr id="rq17">
       <td class='qnum'>17.</td>
       <td class='qtext'>
        Questions? <i>Maximum of 500 characters - 
        <input style="color:red;font-size:12pt;font-style:italic;" readonly="readonly" type="text" id='q17length' name="q17length" size="3" maxlength="3" value="500" /> characters left</i>
        <br />
        <textarea 
              onKeyDown="textCounter(this,'q17length',500);"
              onKeyUp="textCounter(this,'q17length',500)" 
              class="scanwid" name="q17" id="q17" rows="5" cols=""></textarea>
       </td>
    </tr>
    

    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;
    }
    
    0 讨论(0)
提交回复
热议问题