Question:
javascript form?
aryaxt
2007-02-06 11:27:33 UTC
document.myForm.myInput.text = test;





im getyting this error " document.myForm.myInput is null or not an object"1
Four answers:
AnalProgrammer
2007-02-06 12:22:31 UTC
You need to show more code.

The first thing to be aware of is that your web page is executed from the top down.

If the javascript is not in a function then it is executed first before the form has been created.

Thus it is correct to issue the error that you are getting.

To fix this I would suggest that you put the javascript in a function called startUp.

Then code the body tag like this

Klausy
2007-02-06 20:42:36 UTC
Give the input field an ID rather than name. then you can use getElementByID to read the value. Also if start using JS libraries like the Yahoo YUI Library etc you can simply call objects using their simplified DOM collection methods.



Everything that is unique and stores a value or some sort of state info should have an ID. The added value being that you can then also use the ID to better target each element for custom styling via CSS.



in your example above, if you change name to id then the following code would work to get a value and it will work on all modern browsers



document.getElementById("myInput").value = "some test value"
anonymous
2007-02-06 22:04:56 UTC
To expand upon a previous answer:



When you use a name attribute in an HTML element, the Document Object Model (DOM) treats the element as though it is in an array.



Thus, using your current HTML, and assuming your text field is the only element with the name myInput, then you would access it via:



document.myForm.myInput[ 0 ].value



(And note that you assign textbox values with the value property; there is no text property for input in the IE or Mozilla DOMs).



The previous answer is correct; elements can share names but they can't share IDs. So, if you change your textbox to be , your script will work fine in both the IE and Mozilla DOMs.



The name attribute is more commonly used to handle checkboxes / radio buttons, where you often want to return more than one value.



Samples that handle form elements are below.
SkewsMe.com
2007-02-06 19:47:31 UTC
I think myInput.text needs to be myInput.value



The word test needs to be wrapped in quotes.



I think your form needs to precede the call referencing it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...