JS Question

Discussion in 'JavaScript' started by riya_senk, Jun 24, 2007.

  1. #1
    I have little problem with JS so Hope it will be solved here

    There is two input type in one form like

    <input type="checkbox" name="test" value="123">
    <input type="hidden" name="test" value="456">

    and I want to have Value of Both type true javascript

    I did javascript:alert(document.getElementsByName['test'].value); but nothing happen, What should I write to get value of both types.
     
    riya_senk, Jun 24, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Why do they need the same name? You can't work with it this way. If you submit the form, how do you know later on the server which is which?

    You can make an HTML array and send both, like this:
    
    <input type="checkbox" name="test[]" value="123">
    <input type="hidden" name="test[]" value="456">
    
    HTML:
    But anyway, getElementsByName is a function, and therefor it should have parenthesis instead of brackets.

    Also, it will return an array with the 2 fields, so you have to specify which you want to alert().
    
    alert(document.getElementsByName('test')[0].value);
    
    Code (javascript):
    This should work.
     
    nico_swd, Jun 25, 2007 IP
  3. riya_senk

    riya_senk Well-Known Member

    Messages:
    2,014
    Likes Received:
    174
    Best Answers:
    0
    Trophy Points:
    160
    #3
    
    <input type="checkbox" name="test[]" value="123">
    <input type="hidden" name="test[]" value="456">
    
    HTML:

    Actually I am not going to create this form but it is readymade on One web page, I want to get data of that page true JS, That was the format of form which I posted in the thread.
     
    riya_senk, Jun 25, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Then you can take out the brackets, and the line I posted above should work:
    
    alert(document.getElementsByName('test')[0].value);
    // And
    alert(document.getElementsByName('test')[1].value);
    
    Code (javascript):
     
    nico_swd, Jun 25, 2007 IP
    riya_senk likes this.
  5. riya_senk

    riya_senk Well-Known Member

    Messages:
    2,014
    Likes Received:
    174
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Thanks, Problem solved and rep added :)
     
    riya_senk, Jun 25, 2007 IP