Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting maxlength of textbox with JavaScript or jQuery

I want to change the maxlength of a textbox with JavaScript or jQuery: I tried the following but it didn't seem to help:

var a = document.getElementsByTagName('input'); for(var i=0; i<a.length; i++) {     if((a[i].type!= 'radio')||(a[i].type!= 'checkbox'))         a[i].maxlength = 5; } document.getElementsByTagName('input')[1].maxlength="3";  $().ready(function() {     $("#inputID").maxlength(6); }); 

What am I doing wrong?

like image 991
shebelaw Avatar asked May 26 '11 16:05

shebelaw


People also ask

How can set maximum length of textbox in jquery?

maxlength="3"; $(). ready(function() { $("#inputID"). maxlength(6); });

How do I limit the length of a number in Javascript?

To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do you give a Maxlength in HTML?

The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into an <input> or <textarea> . This must be an integer value 0 or higher. If no maxlength is specified, or an invalid value is specified, the input or textarea has no maximum length.


1 Answers

Not sure what you are trying to accomplish on your first few lines but you can try this:

$(document).ready(function() {     $("#ms_num").attr('maxlength','6'); }); 
like image 107
Jay Avatar answered Oct 11 '22 12:10

Jay