View Full Version : little dom question ...

07-31-2006, 12:45 PM
ok, i'm probably stupid for asking this because i thought it'd be easy to do, but i can't get it working. looking for this to work in both mozilla and ie. i need to pass the name of a form element and it's value. when i pass it, it can't find the element. my function (for mozilla) looks like this ("query" is the name of my form):

function queryForm(objID, strVal){
document.query.objID.value = strVal;

do i have to quote it or something, i just don't know what's going on.

07-31-2006, 02:46 PM

or even shorter:

On the other hand, make sure objId is a name, not an id, because the syntax is

document['form_name']['element_name'] -

If you use id, than the reference should be


Keep in mind that name and id are different things.

07-31-2006, 02:55 PM
You may try one of these to get it to work:
Code 1 (objID is the element's name attribute):

function queryForm(objID, strVal){
document.getElementsByName(objID)[0].value = strVal;

Code 2 (objID is the element's id attribute):

function queryForm(objID, strVal){
document.getElementById(objID).value = strVal;

Note both the spelling and caps for each DOM function (eg the getElement{s}By{Name/Id} function). Name attributes do not need to be unique, so the function will return a collection of them (which is why I had your function access the first one (indexed at [0]). The id, on the other hand, is unique, so its function will return that specific object. It may be this non-uniqueness of the name that causes Moz to have trouble with the DOM0 version of accessing the object.

07-31-2006, 03:02 PM
yeah the array worked ... i can't believe i meissed that one.