Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    little dom question ...

    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):
    Code:
    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.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Use JSON:

    document.querry.elements[objId].value=strVal;
    or even shorter:
    document.querry[objId].value=strVal;

    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

    document.getElementById(objId).value=strVal;

    Keep in mind that name and id are different things.
    Last edited by Kor; 07-31-2006 at 02:51 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You may try one of these to get it to work:
    Code 1 (objID is the element's name attribute):
    Code:
    function queryForm(objID, strVal){
    document.getElementsByName(objID)[0].value = strVal;
    }
    Code 2 (objID is the element's id attribute):
    Code:
    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.
    If you want answers, write a smart question.

    Yes, someone probably does know how...

    Oh, and if you want to learn, STFW!

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah the array worked ... i can't believe i meissed that one.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •