Decision.

var self = this; var maxLength = []; $("[maxlength]").each(function () { maxLength.push(parseInt($(this).attr('maxlength'))); }); $('#title_book').keyup(function() { self.keyupListener($('#title_book'), 0); //$('#sub').enabled = self.tryLength(); }) $('#annot_book').keyup(function() { self.keyupListener($('#annot_book'), 1); }) $('#descr_book').keyup(function() { self.keyupListener($('#descr_book'), 2); }) this.keyupListener = function(elem, idx) { var curLength = $(elem).val().length; $(elem).val($(elem).val().substr(0, maxLength[idx])); console.log(parseInt(maxLength[idx])); var remaning = (parseInt(maxLength[idx])) - parseInt(curLength); if (remaning < 0) remaning = 0; $('#number_char' + idx).html(remaning + ' осталось символов'); $('#number_value' + idx).css('display','block'); $('#numberV' + idx).animate({'width': curLength+'%', }, 1); if (remaning < 10) { $('#number_char' + idx).addClass('warning'); } else { $('#number_char' + idx).removeClass('warning'); } return $(elem).attr('maxlength'); } this.tryLength = function() { return $('#title_book').val().length > 2 && $('#annot_book').val().length > 2; } 
  • 3
    @El_ten, so what line does it swear at? also a link or fidl would be useful, for example, I’m trying to penetrate into the cloth of a code torn out with meat, I suspect not only me - Duck Learns to Hide
  • @El_ten, extravaganza on vacation. Do you want us to identify an error on a piece taken out of context? Put a debugger in front of the problem line and see what happens there. - Lucky
  • Hindu code in full growth - Yura Ivanov

1 answer 1

Corrected and working code (animation also works)

1) Do not forget to specify the base of the number system (2nd parameter of the function), when using parseInt ;
2) Check for the presence of the maxlength attribute
3) Check the correctness of the selectors;
4) Check the correct use of styles;
5) Check the correctness of the semicolon;
6) If you ask for help, describe the problem in as much detail as possible, indicate what you did and what did not work out, try to isolate the problem portion of the code;
7) The correct formula for progress bar:

  • Descending: (maxLength [idx] - curLength) / maxLength [idx] * 100 + '%'
  • Increasing: 100 - (maxLength [idx] - curLength) / maxLength [idx] * 100 + '%'