Need proper syntax

Discussion in 'JavaScript' started by npd1164, Dec 7, 2007.

  1. #1
    What's the proper syntax to make this work:

    document.myForm.myField.value = newValue;
    (Where red portions are variables)

    I thought it should be something like:
    document.(eval(myForm)).eval(myField).value = newValue;

    ...but it's not working and cannot seem to find a reference for the proper way to write it. Thanks for any input
     
    npd1164, Dec 7, 2007 IP
  2. hrcerqueira

    hrcerqueira Peon

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    document[myForm][myField].value = newValue;
    
    Code (markup):
     
    hrcerqueira, Dec 7, 2007 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't think you should use that approach because the name attribute in the form tag isn't valid.
     
    MMJ, Dec 7, 2007 IP
  4. hrcerqueira

    hrcerqueira Peon

    Messages:
    125
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It isn't in XHTML, that's true. Usually the best thing to do is to call the form fields by theyr id, but there are many people who still does things this way.
     
    hrcerqueira, Dec 7, 2007 IP
  5. npd1164

    npd1164 Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the feedback and suggestions. Should've had morning coffee before posting. :eek: I resolved this by using:

    document.getElementById(myField).value = newValue;
     
    npd1164, Dec 7, 2007 IP