var for form element name

D

dstefani

Hello,

I'm trying to pass a var to a function for it to be used as the name of
a form element.

IE:

function doThis(form, elemName) {

if( form.elemName.value == false ) {
// do something
}

}

I can't seem to figure out how to use the var where the name goes.

Thanks,

- D
 
B

bobzimuta

i was able to use
function doThis(formName, elemName) {

if( document.forms[formName].elemName.value == false ) {
// do something
}

}
 
T

Thomas 'PointedEars' Lahn

dstefani said:
I'm trying to pass a var to a function for it to be used as the name of
a form element.

IE:

The user agent does not matter, this feature is supported by all JS-capable
HTML UAs (part of the so-called DOM Level 0).
function doThis(form, elemName) {

if( form.elemName.value == false ) {

Use bracket property accessor syntax to access an element of the collection:

if (form.elements[elemName].value)
{

Since the `value' property of objects referring to form controls is of type
string, it can never be `false'. You could test for the empty string (x ==
'' or x.length == 0), or you could just use automatic type conversion as I
did.
// do something
}

}

I can't seem to figure out how to use the var where the name goes.

<http://jibbering.com/faq/#FAQ4_13>


HTH

PointedEars
 
D

dstefani

Excellent, thanks.

I haven't had to do any JS stuff in a while. Serious cobwebs!

Thanks,

- D
 
T

Thomas 'PointedEars' Lahn

Thomas said:
dstefani said:
function doThis(form, elemName) {

if( form.elemName.value == false ) {

Use bracket property accessor syntax to access an element of the
collection:

if (form.elements[elemName].value)
{

Additional note: Since I do not know which value you are passing for `form',
I have to consider that it may be a number or string identifying the form
element by its index, name or ID, instead of a reference to a
HTMLFormElement object. In this case, you would need

if (document.forms[form].elements[elemName].value ...)
{
// ...
}


PointedEars

P.S.: Please quote what you are replying to, see
<http://jibbering.com/faq/>.
 
D

dstefani

Well the above works, but now I'm trying to go back and focus on the
errant field, like so:

<code>

if ( TE_digit(form.elements[elemName].value) == false) {
alert("Yatta, yatta, yatta");
var elem = form.elements[elemName];
elem.focus();
return false;
}
</code>

It still won't focus back on the field that it tested.

What am I missing?

Thanks

- D
 
T

Thomas 'PointedEars' Lahn

dstefani said:
Well the above works,

Which "above" are you talking about?
but now I'm trying to go back and focus on the
errant field, like so:

<code>

if ( TE_digit(form.elements[elemName].value) == false) {
alert("Yatta, yatta, yatta");
var elem = form.elements[elemName];
elem.focus();
return false;
}
</code>

It still won't focus back on the field that it tested.

Probably there is more than one form control with that name in
which case all so-named form controls are members of a collection
returned by form.elements[elemName]. The JavaScript console should
show an script error or Exception on the elem.focus() line then.

Otherwise you will have to show more code, best by posting a public
URI for your HTML document.


PointedEars
 

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

Latest Threads

Top