Count textarea characters

前端 未结 11 1961
走了就别回头了
走了就别回头了 2020-11-28 07:29

I am developing a character count for my textarea on this website. Right now, it says NaN because it seems to not find the length of how many characters are in the field, wh

相关标签:
11条回答
  • 2020-11-28 08:10

    Here is simple code. Hope it is working

    $(document).ready(function() {
    var text_max = 99;
    $('#textarea_feedback').html(text_max + ' characters remaining');
    
    $('#textarea').keyup(function() {
        var text_length = $('#textarea').val().length;
        var text_remaining = text_max - text_length;
    
        $('#textarea_feedback').html(text_remaining + ' characters remaining');
    });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea id="textarea" rows="8" cols="30" maxlength="99" ></textarea>
    <div id="textarea_feedback"></div>

    0 讨论(0)
  • 2020-11-28 08:14

    var maxchar = 10;
    $('#message').after('<span id="count" class="counter"></span>');
    $('#count').html(maxchar+' of '+maxchar);
    $('#message').attr('maxlength', maxchar);
    $('#message').parent().addClass('wrap-text');
    $('#message').on("keydown", function(e){
    	var len =  $('#message').val().length;
    	if (len >= maxchar && e.keyCode != 8)
    		   e.preventDefault();
    	else if(len <= maxchar && e.keyCode == 8){
    		if(len <= maxchar && len != 0)
    	   		$('#count').html(maxchar+' of '+(maxchar - len +1));
    	   	else if(len == 0)
    	   		$('#count').html(maxchar+' of '+(maxchar - len));
    	}else
    		 $('#count').html(maxchar+' of '+(maxchar - len-1)); 
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea id="message" name="text"></textarea>

    0 讨论(0)
  • 2020-11-28 08:18
    textarea.addEventListener("keypress", textareaLengthCheck(textarea), false);
    

    You are calling textareaLengthCheck and then assigning its return value to the event listener. This is why it doesn't update or do anything after loading. Try this:

    textarea.addEventListener("keypress",textareaLengthCheck,false);
    

    Aside from that:

    var length = textarea.length;
    

    textarea is the actual textarea, not the value. Try this instead:

    var length = textarea.value.length;
    

    Combined with the previous suggestion, your function should be:

    function textareaLengthCheck() {
        var length = this.value.length;
        // rest of code
    };
    
    0 讨论(0)
  • 2020-11-28 08:19

    ⚠️ The accepted solution is outdated.

    Here are two scenarios where the keyup event will not get fired:

    1. The user drags text into the textarea.
    2. The user copy-paste text in the textarea with a right click (contextual menu).

    Use the HTML5 input event instead for a more robust solution:

    <textarea maxlength='140'></textarea>
    

    JavaScript (demo):

    const textarea = document.querySelector("textarea");
    
    textarea.addEventListener("input", event => {
        const target = event.currentTarget;
        const maxLength = target.getAttribute("maxlength");
        const currentLength = target.value.length;
    
        if (currentLength >= maxLength) {
            return console.log("You have reached the maximum number of characters.");
        }
    
        console.log(`${maxLength - currentLength} chars left`);
    });
    

    And if you absolutely want to use jQuery:

    $('textarea').on("input", function(){
        var maxlength = $(this).attr("maxlength");
        var currentLength = $(this).val().length;
    
        if( currentLength >= maxlength ){
            console.log("You have reached the maximum number of characters.");
        }else{
            console.log(maxlength - currentLength + " chars left");
        }
    });
    
    0 讨论(0)
  • 2020-11-28 08:19

    They say IE has issues with the input event but other than that, the solution is rather straightforward.

    ta = document.querySelector("textarea");
    count = document.querySelector("label");
    
    ta.addEventListener("input", function (e) {
      count.innerHTML = this.value.length;
    });
    <textarea id="my-textarea" rows="4" cols="50" maxlength="10">
    </textarea>
    <label for="my-textarea"></label>

    0 讨论(0)
提交回复
热议问题