Problem with form.elements.lenght

P

Pudlik, Szymon

Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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.
 
V

VK

Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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
 
M

Michael Winter

On 03/08/2005 17:38, Pudlik, Szymon wrote:

[snip]
if (form.elements.disabled == 1)
form.elements.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.type)) {e.disabled = false;}

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

Hope that helps,
Mike
 
P

Pudlik, Szymon

Michael said:
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.type)) {e.disabled = false;}

/* ...or if('select-one' == e.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.type)){
el.disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el.type){
el.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.
 
P

Pudlik, Szymon

Michael said:
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.type)) {e.disabled = false;}

/* ...or if('select-one' == e.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.type)){
el.disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el.type){
el.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.
 
P

Pudlik, Szymon

Michael said:
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.type)) {e.disabled = false;}

/* ...or if('select-one' == e.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.type)){
el.disabled = false;
}
}
}

and second:

function onSubmit(form){
var el = form.elements;
for (var i = 0; i < el.lenght; i++){
if('select-one' == el.type){
el.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.
 
A

alu

Pudlik said:
Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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
 
G

Grant Wagner

Pudlik said:
Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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.type.indexOf('select') > -1 &&
!f.disabled) {
// if the element type is select-one
// or select-multiple, and it's not currently
// disabled, disable it
f.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.
 
M

Michael Winter

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
 
P

Pudlik, Szymon

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.type){
el.disabled = false;
}
}

}

and it works :)

Thanks.
Szymon Pudlik.
 
C

cosmic foo

Pudlik said:
Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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").getElementsByTagName("*");
 
G

Grant Wagner

cosmic foo said:
Pudlik said:
Hi,

I've written some code:


function onSubmit(form){
for (var i = 0; i < form.elements.lenght; i++){
if (form.elements.disabled == 1)
form.elements.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").getElementsByTagName("*");


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top