How To Display Real Time Characters Count Using Jquery?
Solution 1:
Here is a working fiddle with combining 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);
functionupdateCount() {
$('#characters').text($(this).val().length);
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text"name="name"><spanid="characters"><span>
Solution 2:
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.
<scriptsrc="http://code.jquery.com/jquery-1.11.1.js"type="text/javascript"></script><inputtype="text"id="foo"name=""><spanid="characters"><span><scripttype='text/javascript'>
$('#foo').keyup(updateCount);
$('#foo').keydown(updateCount);
functionupdateCount() {
var cs = $(this).val().length;
$('#characters').text(cs);
}
</script>
Solution 3:
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
Solution 4:
You are not selecting the input field here. Try the following
$('input').keyup(updateCount);
$('input').keydown(updateCount);
Solution 5:
Here you go with a solution https://jsfiddle.net/mLb41vpo/
$('input[type="text"]').keyup(updateCount);
functionupdateCount() {
var cs = $(this).val().length;
$('#characters').text(cs);
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><inputtype="text"name=""><spanid="characters"><span>
Mistake was $('text').keyup(updateCount);
, you can refer input textbox using 'text'.
It should be $('input').keyup(updateCount);
Post a Comment for "How To Display Real Time Characters Count Using Jquery?"