[thelist] form reference conundrum

Chris Marsh chris at webbtech.co.uk
Wed Jan 8 12:03:06 CST 2003


Tom

> I'm trying to access an element in my form to write a value
> to it. I'm using the DOM lvl 0 as I always do, yet its not
> working and it seems it should..maybe someone can point out my error.
>
> Here is the form:
> --------------------
> <form name="cartadd" method="POST" action="/preCart.php"
> style="display: inline; height: auto;" onsubmit="track(this)">
>
> <input type="hidden" name="cust" value="">
> //more elements
> </form>
> --------------------
>
> I want to access 'cust', the hidden element. I am doing this:
>
> -------------------
> oForm = document.forms["cartadd"];
> custHidden = oForm.elements["cust"]; <-- errors as null or not object
> -------------------
>
> oForm returns an object for sure in an alert. 'cust' is
> indeed the name of the element. There are no other forms on
> this page named cartadd, nor any other form elements named
> cust... I can't figure this out!

I created a very simple html page containing only the form information,
and a function called Test() which contained the javascript alerting the
value of custHidden. The test function was executed upon clicking a link
in the HTML. This behaved as one would expect, displaying [object] in
the alert. Are you executing the javascript *after* the page has loaded?
If the javascript is inline, then the field will not necessarily exist
at runtime.

HTH

Regards

Chris Marsh






More information about the thelist mailing list