Forum Moderators: open
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
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]
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.
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?
Nothing's working!
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
NB: this is case-sensitive, it must be
maxLengthwith 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!
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.