Forum Moderators: open

Message Too Old, No Replies

Change maxlength of all inputs

what code do you use to change the maxlength on all inputs on a page?

         

Xapti

12:07 am on Sep 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a pretty simple request in my opinion, I'm just not very good with javascript since I hardly use it, and am not familiar with all the elements and syntaxes.
What I want is for some javascript code to modify all inputs (type = text, but doesn't matter) to have a new maxlength value. They can all be assigned the same value.

Arno_Adams

6:42 am on Sep 10, 2007 (gmt 0)

10+ Year Member



Hi,

How's this:


function foo(int) {
var a = document.getElementsByTagName('input');
for(var i=0; i<a.length; i++) {
a[i].maxlength = int;
}
}

HTH, AA

Xapti

1:45 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh damn, I should have been able to wruite up that code myself, but I gfuess i wasn't confident enough in my aBilities. Nonetheless greatly appreciated.

phranque

4:27 am on Sep 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you might want to add a test to see if it's actually a text type input element before setting maxlength as per your stated requirements...

Arno_Adams

6:47 am on Sep 11, 2007 (gmt 0)

10+ Year Member



Hi,

You're welcome. And phranque is correct, make sure you don't set maxlengths on radiobuttons and checkboxes.

An updated version:


function foo(int) {
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 = int;
}
}

HTH, AA

Xapti

9:45 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you are using negation comparisons, you should use AND instead of OR. As it is now it will trigger high if either one is true. considering it's applying to type=text, I would think the best comparison would just be
if(a[i].type= 'text')

[edited by: Xapti at 9:46 pm (utc) on Sep. 11, 2007]

Xapti

11:18 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't seem to be working. Using the code with or without the if statement seems to do nothing (maxlength is still the same). The if statement I used was:
if(a[i].type == 'text'){
a[i].maxlength = int;
}
perhaps the if statement I wrote is wrong?
yes when I called the function I wrote the number i wanted instead of 'int'.

Maybe I should do a for i in a loop, since it should do the same thing as using i<a.length anyway? probably doesn't matter?

[edited by: Xapti at 11:41 pm (utc) on Sep. 11, 2007]

daveVk

12:43 am on Sep 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is it the size of the box or the maxlength of text that can be entered you wish to set. If the former replace "maxlength" with "size"

Xapti

2:30 am on Sep 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I obviously do not want the size.

daveVk

5:26 am on Sep 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Silly idea 2.

Try var a = document.getElementsByTagName('INPUT');

That is upper case, think it case sensitive.

penders

9:51 am on Sep 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Unfortunately I have read (but not tested) that any attempts to dynamically change the value of maxlength in **IE** are ignored!? Apparently, "the maxlength attribute is only processed by the rendering engine when the page first loads". Should however work OK in FF.

However, before giving up, I would also try:

a[i].setAttribute('maxlength',int);

Or even try setting 'maxLength' (with a capital 'L')?!

The workaround I have seen is to write your own maxlength function and hook it to the onkeypress event of the textbox.

Xapti

12:36 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't even tried in IE, only FF, so that isn't the issue.
is JS really case sensitive for attributes? I guess it's related to C, and C is case-sensitive, so I'd guess the answer is yes. That may very well then be the issue.

I know the getelementsbytagname(lowercase input) worked though, since I had a bug (fixed) with it where I saw it affected the inputs, but not in the right way! Also, the input tags aren't in uppercase in the code.

I have been using just maxlength, not MaxLength or maxLength maybe that is a problem?

Xapti

1:44 am on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've tried a[i].setAttribute('maxLength',int);
as well as the other method with the uppercase L.
I have also tried
var snapMaxLength = document.evaluate("//input[@type='text']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
not that the other method didn't work, so kinda pointless anyways, but did it just in case.

Nothing's working!

Arno_Adams

8:04 am on Sep 13, 2007 (gmt 0)

10+ Year Member



Hi Xapti,

Try this; I've tested it and it works fine;


<script type="text/javascript">
function foo(int) {
var a = document.getElementsByTagName('input');
for(var i=0; i<a.length; i++) {
if(a[i].type == 'text') {
a[i].maxLength = int;
}
}
}
</script>
</head>

<body>
<form name="form1" id="form1" method="post" action="">
<input type="text" name="fld1" id="fld1" maxlength="5" size="10"><br>
<input type="text" name="fld2" id="fld2" maxlength="5" size="10"><br>
<input type="text" name="fld3" id="fld3" maxlength="5" size="10">
<button type="button" onClick="foo(8);">Go</button>
</form>

HTH, AA

penders

12:06 pm on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just to confirm, Arno_Adams solution works OK for me too in FF2, IE6 and Op9.

NB: this is case-sensitive, it must be

maxLength
with a capital 'L'. If it's all lowercase then it won't work in any browser.

Out of curiosity I tried the various methods of setting the maxlength attribute from JS and this is what I found:

// OK: FF2, IE6, Op9
a[i].maxLength = int;

// Lowercase - Not work in any browser!
a[i].maxlength = int;

// OK: FF2, IE6, Op9
a[i].setAttribute('maxLength',int);

// OK: FF2, Op9 - BUT NOT IE6!
a[i].setAttribute('maxlength',int);

I know the getelementsbytagname(lowercase input) worked though...

I'm not sure how this would have worked - JS is case-sensitive. This produces a JS error in FF2, IE6 and Op9 and halts code execution.

Unfortunately I have read (but not tested) that any attempts to dynamically change the value of maxlength in **IE** are ignored!? Apparently, "the maxlength attribute is only processed by the rendering engine when the page first loads".....
....The workaround I have seen is to write your own maxlength function and hook it to the onkeypress event of the textbox.

Just in case anyone is dwelling on this comment I made above - it's just plain wrong!

Xapti

1:27 pm on Sep 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I said the getelementsbytagname(lower case input) I mean just as I said it, not as verbatim in typing.
In a case where I posted my current code you can see I used getElementsByTagName('input'). I was referring to the input being lower case letters, since somone suggested them being all uppercase.

I should have maybe mentioned the following earlier, but I don't see how it was relevant until now (now that the posted code doesn't work for me, but works for others): I'm using this script for greasemonkey. So I wouldn't need it as a function, since I'd just call it right after it's declared anyways. Still doesn't work in either form though.

Just an FYI though, the verbatim code recently posted which was said to work I have not tried yet since I'm on a different computer and location at the moment. As far as I recall though, it looks about the same. I will update again once I try the code verbatim.

Xapti

12:28 am on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ohkay I made a silly oversight.
The elements are created with javascript, so if the code was executed before the inputs were created, it would not work! it is fixed now, I call the function after the inputs are created, and things work now. Thanks for everyone who helped.

Arno_Adams

7:02 am on Sep 14, 2007 (gmt 0)

10+ Year Member



oh,

that's a requirement you forgot to mention before.
Glad to hear it's working now.

AA