Input field length

Is there a way with jQuery to find out the width of the text inserted in the text input field of the text? I need this information because I need to do some work when possible. It would also be very useful for me to find the jQuery event that occurs when the user enters another character in the input field that will make the first character of the integer value invisible?

See an 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 typing 123456 in the input field. When you enter char 6, the first char 1 will be invisible.

I need this event when the value overlaps the input.

+6
source share
8 answers

You can find the length of the value using the jQuery val () method, which returns the current value of the form element as a string. Then you can use the length property from this line.

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

Here is a working jsFiddle example: http://jsfiddle.net/J58ht/

on your edited question it should be 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); } }); 
+8
source

Try using:

 var LengthOfText = $("#txtInputText").val().length; 
+3
source

You can use length

  jQuery(selector).val().length; 

For instance,

  $('#id').val().length; $('#class name').val().length; 
+2
source

try it

 $("#txtInputText").val().length 

.length

+1
source

try it

 var inputLen = $("#txtInputText")[0].length 
0
source

you can try

  $("#target").val().length; 

And remember that length not a function.

0
source
 var value = $("#textFieldId").val(); var fieldLength = value.length 

It works.

0
source
 To find out the length of text you can use var len = $("#id").val().length; $("#id").keyup(function(){ var textLength = $(this).val().length; if(textLength > 15){ alert('Length is exceeded'); //Do ur stuff; } }); 
0
source

Source: https://habr.com/ru/post/944524/


All Articles