Hello. I decided in the admin to make a text field with a character counter (that is, enter something, and you will see how many characters you entered from the bottom). With jquery, I pull the number of characters in the field, but I don’t know how to make the number of characters increase when typing.

    2 answers 2

    Hang up on the onkeyup event of your control.

    C html:

     <textarea id="memo"></textarea> <div id="counter"></div> 

    on jQuery, it looks like this:

     jQuery(function($){ $('#memo').keyup(function(){ $('#counter').text( $('#memo').val().length ); }); }); 
    • the value can be obtained through val: $ ('# memo'). val (). length - aachurin
    • And usually (I really think that I don’t quite catch up with why) write something like this: (function ($) {/ * ... /}) (jQuery); *those. I'm catching up, it just seems that there is clearly something besides beauty here) - Sh4dow
    • Thanks for the detailed answer. Exactly what you need. Really helped! - ouv
    • @ Sh4dow, it's simple. $ uses not only jQuery, but also other libraries as a reference to its singleton. And, with the help of (function ($) {/ * ... * /}) (jQuery); $ shielded inside a separate scope. And the possible jQuery != $ conflict jQuery != $ reduced to nothing. If you write everything down with classical constructions, you will get it more clearly: function foo ($) {/ * ... * /} foo (jQuery); @aachurin, yes, that's right. Thank. - KiTE
    • And: jQuery (function () {/ * ... /}); this is the same as: jQuery (document) .ready (function () {/ ... * /}); but, imkho, more laconic option of record. - KiTE

    It's simple. On $.keyup - count the number of characters in the text using length http://api.jquery.com/length/