467,923 Members | 1,436 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Problem with form.elements.lenght

Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

....

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

....

</form>
I want to turn on all of <select> elements on my form by set their field
'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?

Greetings, Szymon Pudlik.
Aug 3 '05 #1
  • viewed: 7035
Share:
12 Replies
VK

Pudlik, Szymon wrote:
Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

...

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

...

</form>
I want to turn on all of <select> elements on my form by set their field
'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?

Greetings, Szymon Pudlik.

document.forms[0].elements[i]

Aug 3 '05 #2
On 03/08/2005 17:38, Pudlik, Szymon wrote:

[snip]
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
The disabled property is a boolean, not a number.

[snip]
I want to turn on all of <select> elements on my form by set their
field 'disabled' to false.
Your code would enable all form controls, not just SELECT elements.
I've tried to get form.elements.lenght's value by putting it to alert
box. IE (NS also) said "undefined". Where is the problem?


You're spelling length incorrectly.

function onSubmit(f) {
var e = f.elements,
i, n;

for(i = 0, n = e.length; i < n; ++i) {
if(/^select/.test(e[i].type)) {e[i].disabled = false;}

/* ...or if('select-one' == e[i].type) {...} */
}
}

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 3 '05 #3
VK wrote:
document.forms[0].elements[i]


Hi,
it is also not working. I've tried this before I've written on this group...

Cheers, Szymon.
Aug 3 '05 #4
alu

"Pudlik, Szymon" <sp*****@CUTTHISdesy.de>
Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

...

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

...

</form>
I want to turn on all of <select> elements on my form by set their field
'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?

Greetings, Szymon Pudlik

Spelling error. All instances of 'length' are spelled incorrectly.
-alu
Aug 3 '05 #5
"Pudlik, Szymon" <sp*****@CUTTHISdesy.de> wrote in message
news:dc***********@claire.desy.de...
Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

...

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

...

</form>
I want to turn on all of <select> elements on my form by set their
field 'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?


length, not lenght.

Also, the disabled property contains a boolean, not an number. Although
type conversion will probably make the code work, it's best to stick to
the type the property expects. As well, it appears you are testing to
see whether every element is disabled, and if it is, you are enabling
it. If all you want is to disable <select> elements, you should be
testing for them before disabling every element on the page:

function onSubmit(f) {
f = f.elements;
for (var i = 0; i < f.length; ++i) {
if (f[i].type.indexOf('select') > -1 &&
!f[i].disabled) {
// if the element type is select-one
// or select-multiple, and it's not currently
// disabled, disable it
f[i].disabled = true;
}
}
}

Be aware, if something goes wrong during the form submission, you have
now left the user with a useless form because all the <select> elements
are disabled.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Aug 3 '05 #6

Michael Winter wrote:
You're spelling length incorrectly.
You are right. I've done a mistake.
function onSubmit(f) {
var e = f.elements,
i, n;

for(i = 0, n = e.length; i < n; ++i) {
if(/^select/.test(e[i].type)) {e[i].disabled = false;}

/* ...or if('select-one' == e[i].type) {...} */
}
}


I've tried first option:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if(/^select/.test(el[i].type)){
el[i].disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el[i].type){
el[i].disabled = false;
}
}
}

and both doesn't work. All my select items are still disabled :(
Is it possible that I do not understand what is "/^select/" and
'select-one'.. Would you like to explain me that?

Greetings, Szymon Pudlik.
Aug 3 '05 #7

Michael Winter wrote:
You're spelling length incorrectly.
You are right. I've done a mistake.
function onSubmit(f) {
var e = f.elements,
i, n;

for(i = 0, n = e.length; i < n; ++i) {
if(/^select/.test(e[i].type)) {e[i].disabled = false;}

/* ...or if('select-one' == e[i].type) {...} */
}
}


I've tried first option:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if(/^select/.test(el[i].type)){
el[i].disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el[i].type){
el[i].disabled = false;
}
}
}

and both doesn't work. All my select items are still disabled :(
Is it possible that I do not understand what is "/^select/" and
'select-one'.. Would you like to explain me that?

Greetings, Szymon Pudlik.

Aug 3 '05 #8

Michael Winter wrote:
You're spelling length incorrectly.
You are right. I've done a mistake.
function onSubmit(f) {
var e = f.elements,
i, n;

for(i = 0, n = e.length; i < n; ++i) {
if(/^select/.test(e[i].type)) {e[i].disabled = false;}

/* ...or if('select-one' == e[i].type) {...} */
}
}


I've tried first option:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if(/^select/.test(el[i].type)){
el[i].disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el[i].type){
el[i].disabled = false;
}
}
}

and both doesn't work. All my select items are still disabled :(
Is it possible that I do not understand what is "/^select/" and
'select-one'.. Would you like to explain me that?

Greetings, Szymon Pudlik.
Aug 3 '05 #9
On 03/08/2005 18:27, Pudlik, Szymon wrote:
Michael Winter wrote:
You're spelling length incorrectly.
You are right. I've done a mistake.


And you're still doing it. There is no lenght property (anywhere in the
DOM) so el.lenght evaluates to undefined. In relational comparisons such as

i < el.lenght

undefined operands cause the expression to evaluate to false. This means
that your loop is never entered.

Spell length properly as it is in my original code (or just use it
verbatim); it ends 'th', not 'ht'.

[snip]
Is it possible that I do not understand what is "/^select/" and
'select-one'.. Would you like to explain me that?


All form controls have a type property that reflects the type of
control. SELECT elements have two possible values: select-one and
select-multiple. When the boolean attribute, multiple, is added to a
SELECT element, the type property will contain the latter (select-multiple).

I assumed that you're using singular SELECT elements (rendered as a
drop-down, not a list), so I suggested a direct test of 'select-one' as
that should be quicker. But, just in case, the regular expression,

/^select/

could be used to check for either of the two values (it will match any
string that begins [^] with the sequence, select).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Aug 3 '05 #10

"Pudlik, Szymon" <sp*****@CUTTHISdesy.de> wrote in message
news:dc***********@claire.desy.de...
Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

...

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

...

</form>
I want to turn on all of <select> elements on my form by set their field
'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?

Greetings, Szymon Pudlik.


just use this instead,

document.getElementById("yourformidhere").getEleme ntsByTagName("*");
Aug 3 '05 #11
Thanks for all answers.

I do this like that:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.length; i++){
if('select-one' == el[i].type){
el[i].disabled = false;
}
}

}

and it works :)

Thanks.
Szymon Pudlik.
Aug 4 '05 #12
"cosmic foo" <so*******@fromnothing.com> wrote in message
news:5w*******************@news20.bellglobal.com.. .

"Pudlik, Szymon" <sp*****@CUTTHISdesy.de> wrote in message
news:dc***********@claire.desy.de...
Hi,

I've written some code:
function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements[i].disabled == 1)
form.elements[i].disabled = 0;
}
}

...

<form action="step3.jsp" method="get" name="myForm"
onsubmit="onSubmit(this)">

...

</form>
I want to turn on all of <select> elements on my form by set their
field
'disabled' to false. Loop doesn't work. I've tried to get
form.elements.lenght's value by putting it to alert box. IE (NS also)
said "undefined". Where is the problem?

Greetings, Szymon Pudlik.


just use this instead,

document.getElementById("yourformidhere").getEleme ntsByTagName("*");


Why not use document.forms[...].elements[...] when it is going to work
in more browsers than your example, since it works in any browser that
supports your example, as well as a collection of other browsers.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Aug 5 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lee Mundie | last post: by
2 posts views Thread by Kevin Auch | last post: by
reply views Thread by Nikolay Petrov | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.