1

I want to truncate the input value by the available space for letters on the screen.

For example, if my input field can contain only 5 letters and after that the input 'scrolls' to the right (overflow) I want jQuery to limit only to 5 letters because that's the available and visible space to the user.

Bottom line - I want jQuery to truncate to the visible letters according to the input's overflow and remove the letters which are not visible.

Example:

enter image description here

I want jquery to truncate the text only to Lorem Ips although the value is actually Lorem Ipsum

How can I do that?

6
  • 1
    have you tried the maxlength attribute? <input type="text" maxlength="5" /> Commented Aug 3, 2017 at 11:59
  • Show your code. Commented Aug 3, 2017 at 12:00
  • maxlength is not an option since I have lots of lots of inputs with different widths Commented Aug 3, 2017 at 12:01
  • I suppose you are looking to find the available width of the input with regard to the character width? If so, this has been answered at: stackoverflow.com/a/118251/2024411 Commented Aug 3, 2017 at 12:02
  • @NitinSuri I guess so Commented Aug 3, 2017 at 12:04

2 Answers 2

1
$(document).ready(function(){
    $("#id_of_textbox").attr('maxlength','5');
});
Sign up to request clarification or add additional context in comments.

2 Comments

I have hundreds of inputs each with a different width..so max length is not possible
@Broshi : You can do can you share your html
0

I have a snippet ready as you requested, thanks for the author of this codepen snippet

If you think this answer as helpful, please give some credit to the author of above codepen snippet too.

///////////////////////////////////////////////
// Source: https://codepen.io/Momciloo/pen/bpyMbB
///////////////////////////////////////////////
$.fn.textWidth = function(text, font) {

  if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);

  $.fn.textWidth.fakeEl.text(text || this.val() || this.text() || this.attr('placeholder')).css('font', font || this.css('font'));

  return $.fn.textWidth.fakeEl.width();
};
///////////////////////////////////////////////

$("input").on("input", function(e) {
  var tWidth = $(this).textWidth(),
      cWidth = $(this).width();
  if (tWidth > cWidth) {
    $(this).val($(this).data("val"));
  } else {
    $(this).data("val", $(this).val());
  }
});
input {
  clear: both;
  float: left;
}

.i1 {
  width: 12em;
}

.i2 {
  width: 6em;
}

.i3 {
  width: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="i1" />
<input type="text" class="i2" />
<input type="text" class="i3" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.