javascript - How to display real time characters count using jQuery? - Stack Overflow

Please check my code below. I want to display input characters number real time using jquery javascript

Please check my code below. I want to display input characters number real time using jquery javascript. But problem is when i am doing it with "textarea" it works but when i do same with normal input type text its not work.

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
      <script src=".11.1.js" type="text/javascript"></script>
</head>
<body>

<!-- Works  -->

<!-- <textarea></textarea>
<span id="characters"><span>

    <script type='text/javascript'>
        $('textarea').keyup(updateCount);
        $('textarea').keydown(updateCount);

        function updateCount() {
            var cs = $(this).val().length;
            $('#characters').text(cs);
        }
    </script> -->

<!-- Not works -->

<input type="text" name="name">
<span id="characters"><span>

<script type='text/javascript'>
        $('text').keyup(updateCount);
        $('text').keydown(updateCount);

        function updateCount() {
            var cs = $(this).val().length;
            $('#characters').text(cs);
        }
 </script>

</body>
</html>

Please check my code below. I want to display input characters number real time using jquery javascript. But problem is when i am doing it with "textarea" it works but when i do same with normal input type text its not work.

<!DOCTYPE html>
<html>
<head>
    <title>Test</title>
      <script src="http://code.jquery./jquery-1.11.1.js" type="text/javascript"></script>
</head>
<body>

<!-- Works  -->

<!-- <textarea></textarea>
<span id="characters"><span>

    <script type='text/javascript'>
        $('textarea').keyup(updateCount);
        $('textarea').keydown(updateCount);

        function updateCount() {
            var cs = $(this).val().length;
            $('#characters').text(cs);
        }
    </script> -->

<!-- Not works -->

<input type="text" name="name">
<span id="characters"><span>

<script type='text/javascript'>
        $('text').keyup(updateCount);
        $('text').keydown(updateCount);

        function updateCount() {
            var cs = $(this).val().length;
            $('#characters').text(cs);
        }
 </script>

</body>
</html>
Share edited Aug 9, 2017 at 14:08 Ritesh Nair 3,3551 gold badge18 silver badges25 bronze badges asked Aug 9, 2017 at 12:41 Nix LiNix Li 572 silver badges7 bronze badges 1
  • text is not a valid selector. – rrk Commented Aug 9, 2017 at 12:43
Add a ment  | 

7 Answers 7

Reset to default 4

Here is a working fiddle with bining your two events keyup and keydown into one line :-)

Your selector was wrong, text doesn't exist. So I call input[name="name"] instead to get the input by your name value:

$('input[name="name"]').on('keyup keydown', updateCount);

function updateCount() {
  $('#characters').text($(this).val().length);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name">
<span id="characters"><span>

$("input").keyup(function(){
$("#characters").text($(this).val().length);
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="name">
<span id="characters"><span>

With "textarea" version you are selecting "textarea" by $('textarea').keyup(updateCount) and $('textarea').keydown(updateCount) nicely but with text input you are doing wrong to select input text.

I have fix it by placing a id called "foo" on input text. This should be working now.

<script src="http://code.jquery./jquery-1.11.1.js" type="text/javascript"></script>
    <input type="text" id="foo" name="">
    <span id="characters"><span>
    
    	<script type='text/javascript'>
            $('#foo').keyup(updateCount);
            $('#foo').keydown(updateCount);
    
            function updateCount() {
                var cs = $(this).val().length;
                $('#characters').text(cs);
            }
        </script>

If you want to get an input with the type text you must use a selector like this

$('input[type="text"]').keyup(updateCount);
$('input[type="text"]').keydown(updateCount);

Here is a list of all jQuery selectors

You are not selecting the input field here.
Try the following

 $('input').keyup(updateCount);
 $('input').keydown(updateCount);

Here you go with a solution https://jsfiddle/mLb41vpo/

$('input[type="text"]').keyup(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#characters').text(cs);
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" name="">
<span id="characters"><span>

Mistake was $('text').keyup(updateCount);, you can refer input textbox using 'text'.

It should be $('input').keyup(updateCount);

Everything is correct in your code, you just need to add ":" beore your type. This will make it identify, it is input type.

Example:

<script type='text/javascript'>
        $(':text').keyup(updateCount);
        $(':text').keydown(updateCount);

        function updateCount() {
            var cs = $(this).val().length;
            $('#characters').text(cs);
        }
 </script>

I changed your script only. Just find the change, you will get it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744918073a4600959.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信