jquery - Input field value's length -


is there way jquery find out width of text inserted in input field of type text? need info because need perform work when achieved. helpful me find jquery event occurring when user entering 1 more character in input field make first character of whole value invisible?

please see example http://jsfiddle.net/j58ht/2/

<input style="width: 35px;" type="text"> <span></span>       $('input').on('keyup',function(){       var input = $(this);       input.next("span").text(input.val().length + " chars"); }); 

try entering characters 123456 in input field. when entering char 6 first char 1 invisible.

i need event, when value overlaps input.

you can find length of value using jquery's val() method returns current value of form element string. can use length property string.

$('input').on('keyup',function(){       alert('this length ' + $(this).val().length); }); 

here's working example on jsfiddle: http://jsfiddle.net/j58ht/

based on edited question should like

$('input').on('keyup',function(){       var my_txt = $(this).val();       var len = my_txt.length;       if(len > my_constant_length)       {           var res = my_txt.substring(1,my_constant_length);           $(this).val(res);       } }); 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -