document.formname wildcard?

J

JV

I thought I once saw somewhere that a global variable, "xyz" for
example, could be declared and used so that instead of using
"document.formname.elementname.value" one could use
"xyz.elementname.value" or use "xyz" in place of "document.formname"
anytime a reference to a form element was made.

Is this actually true or not? I'm going crazy trying to recall
something that might not even exist!

Thanks.

JV
 
A

ASM

JV a écrit :
I thought I once saw somewhere that a global variable, "xyz" for
example, could be declared and used so that instead of using
"document.formname.elementname.value" one could use
"xyz.elementname.value" or use "xyz" in place of "document.formname"
anytime a reference to a form element was made.

Is this actually true or not? I'm going crazy trying to recall
something that might not even exist!

function verif() {
var f = document.forms['myForm']; // shortcut for form named 'myForm'
for(var i=0; i<f.length; i++)
if(f.type == 'text' && f.value=='') {
alert('field "'+f.name+'" not filled');
f.focus();
f.select();
return false;
}
return true;
}
 
E

Evertjan.

ASM wrote on 09 dec 2006 in comp.lang.javascript:
JV a écrit :

that shortcut is browser specific, I heard.
Is this actually true or not? I'm going crazy trying to recall
something that might not even exist!

function verif() {
var f = document.forms['myForm']; // shortcut for form named 'myForm'

shouldn't that be for completeness:

var f = document.forms['myForm'].elements

=========================

Stipulating that you use the return false/true from an onsubmit='..',
"this" is a good alternative, making the function universal:

<form onsubmit='verif(this)' ...
....
function verif(fm) {
var f = fm.elements
for(var i=0; i<f.length; i++)
if(f.type == 'text' && f.value=='') {
alert('field "'+f.name+'" not filled');
f.focus();
f.select();
return false;
}
return true;
}

=========================

What about this:

function verif(fm) {
var f = fm.elements;
i=0;
while (i<f.length && (f.type!='text' || f.value!=''))
i++;
if (i==f.length) return true;
alert('field "'+f.name+'" not filled');
f.focus();
f.select();
return false;
}
 
A

ASM

Evertjan. a écrit :
ASM wrote on 09 dec 2006 in comp.lang.javascript:
var f = document.forms['myForm']; // shortcut for form named 'myForm'

shouldn't that be for completeness:

var f = document.forms['myForm'].elements

yeap, probably, but ...
Stipulating that you use the return false/true from an onsubmit='..',

clever you are that finding :)
"this" is a good alternative, making the function universal:

each thing at its time
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top