I have a piece of code

$('[maxlength][minlength]').focus( function(){ $(this).after('<div></div>');obj=$(this).parent().find('div') } ).bind('keyup focus', function(){ if ($(this).attr('minlength')>this.value.length){ obj.text(($(this).attr('minlength')-this.value.length)+' символов Π½ΡƒΠΆΠ½ΠΎ'); }else{ obj.text(($(this).attr('maxlength')-this.value.length)+' символов ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ'); if (this.value.length>$(this).attr('maxlength')){ this.value=this.value.substring(0,this.getAttribute('maxlength')); } } } ).blur( function(){ obj.remove(); } ); 

Its essence is that when focusing on objects in which there are maxlength and minlength attributes, after them a DIV is created which displays how many characters are needed and left.

The essence of the problem is that this script only works on objects created in advance (put in $(document).ready() ), and I need it to always work.

    1 answer 1

    Try using the live () method. Those. The code will look like this:

     $('[maxlength][minlength]').live('focus', function(){ $(this).after('<div></div>');obj=$(this).parent().find('div') } ).live('keyup focus', function(){ if ($(this).attr('minlength')>this.value.length){ obj.text(($(this).attr('minlength')-this.value.length)+' символов Π½ΡƒΠΆΠ½ΠΎ'); }else{ obj.text(($(this).attr('maxlength')-this.value.length)+' символов ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ'); if (this.value.length>$(this).attr('maxlength')){ this.value=this.value.substring(0,this.getAttribute('maxlength')); } } } ).live('blur', function(){ obj.remove(); } );