Reference form elements 
Author Message
 Reference form elements

Hi All,

I am having problems accessing form elements that have been dynamically
created with server side script.

eg.

in the form
<input type=hidden name='idperson0' value='2'>

in my JavaScript code

function showValue()
{
    var elementName = "idperson0"  // not actually asigned this way - but it
will do for illustration
    alert(document.forms[0].elementName.value)

Quote:
}

I get an error saying the elementName is not an object

Any ideas on how I can get around this?

Cheers



Fri, 07 Feb 2003 03:00:00 GMT  
 Reference form elements
Hi Hugh

document.forms[0].elements[elementName].value;

Now you can use the variable elementName to reach the desired control among the
form's elements array. Other way to accomplish the same would be to use eval(),
but this is better IMHO.

Klaus

Quote:

> Hi All,

> I am having problems accessing form elements that have been dynamically
> created with server side script.

> eg.

> in the form
> <input type=hidden name='idperson0' value='2'>

> in my JavaScript code

> function showValue()
> {
>     var elementName = "idperson0"  // not actually asigned this way - but it
> will do for illustration
>     alert(document.forms[0].elementName.value)
> }

> I get an error saying the elementName is not an object

> Any ideas on how I can get around this?

> Cheers



Fri, 07 Feb 2003 03:00:00 GMT  
 Reference form elements


Quote:
> Hi Hugh

> document.forms[0].elements[elementName].value;

document.forms[0][elementName].value

should suffice.

Quote:
> Now you can use the variable elementName to reach the desired control
among the
> form's elements array. Other way to accomplish the same would be to use
eval(),
> but this is better IMHO.

I agree completely, never use eval() for dynamic object referencing, that's
what array notation is there for.

--
Thor Larholm

Quote:

> > Hi All,

> > I am having problems accessing form elements that have been dynamically
> > created with server side script.

> > eg.

> > in the form
> > <input type=hidden name='idperson0' value='2'>

> > in my JavaScript code

> > function showValue()
> > {
> >     var elementName = "idperson0"  // not actually asigned this way -
but it
> > will do for illustration
> >     alert(document.forms[0].elementName.value)
> > }

> > I get an error saying the elementName is not an object

> > Any ideas on how I can get around this?

> > Cheers



Sat, 08 Feb 2003 10:08:20 GMT  
 Reference form elements
< :-) >Thanks for the compliment, Thor.</ :-) >


Sat, 08 Feb 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. referencing a form element by a derived string

2. Finding # of specific element in forms elements collection

3. Equivalent of document.form.elements[x] on page w/o form

4. check for non existent form element Request.Form

5. Referencing HTML elements in Netscape

6. Dynamically referencing an element?

7. jscript vs javascript problem referencing element

8. element properties can hold object references

9. Referencing an element in a frame

10. XML Reference Element

11. How to reference an event-raising element in a frame based site

12. Referencing elements by ID

 

 
Powered by phpBB® Forum Software