I have two sets of textboxes which are generated dynamically. All these textboxes has either (**textnormal** or **textnormalHS**) as class name and all of them have a specific text (“txtMem”) as part of their ID.
I want to limit the no of characters to be displayed in the texbox to 2.
$("input[type='text'][id*='txtMem']").keyup(function () {
var $this = $(this);
if ($this.val().length > 2) $this.val($this.val().substr(0, 2));
});
doesn’t seem to be working.
Just set the
maxlengthattribute to 2 for all those textboxes you don’t need any JavaScript or jQuery for that.If you want to set it programmatically you can try this.
Note that I am using
input:textwhich is similar toinput[type='text']but it is simple.