OnChange Help

Discussion in 'JavaScript' started by Karl Evans, Apr 12, 2007.

  1. #1
    Evening,

    I'm having some difficulty at the moment with a bit of javascript, I'm by no means an expert, but I'm having a go.

    Right, I'm pulling some information from a database using PHP, I'm storing this in a javascript array. So it's a little like this:

    
      var comments = new array();
    
    Code (markup):
    Then the PHP puts the information in the array like so.

    
      comments['1'] = "Comments Here";
      comments['2'] = "Comments Here Also";
    
    Code (markup):
    Now what I require is a function that will change the data when the corresponding option is selected in the dropdown list.

    
      <select name="comments">
        <option value="1">Change To comments['1']</option>
        <option value="2">Change To comments['2']</option>
      </select>
    
    HTML:
    Whatever is selected needs to be displayed underneath the dropdown list.

    Could anybody put me in the right direction please?

    If somebody can write the function that works, I will chuck them a few quid/dollars via PayPal.

    Thanks,
    Karl
     
    Karl Evans, Apr 12, 2007 IP
  2. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Try below. Note that Array should be capitalised.

    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <script type="text/javascript">
    var comments = new Array();
    comments[1] = "comment 1";
    comments[2] = "comment 2";
    </script>
    </head>
    <body>
    <select name="comments" onchange="document.getElementById('output').innerHTML=comments[this[this.selectedIndex].value];">
        <option value="1">Change To comments['1']</option>
        <option value="2">Change To comments['2']</option>
    </select>
    
    <span id="output"></span>
    </body>
    </html>
    
    HTML:
     
    phper, Apr 12, 2007 IP
  3. Karl Evans

    Karl Evans Peon

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the code.

    However, it outputs

    [object HTMLOptionElement]
    Code (markup):
    Any ideas?

    Thanks,
    Karl
     
    Karl Evans, Apr 12, 2007 IP
  4. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    hmm.. that's weird.
    do you want to check if you copied the brackets correctly?
    have you tried running just the code above? then if it's working without your php code we can move on and check your php code.
     
    phper, Apr 12, 2007 IP
  5. Karl Evans

    Karl Evans Peon

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Works perfectly now.

    Thanks very much!
     
    Karl Evans, Apr 12, 2007 IP
  6. briansol

    briansol Well-Known Member

    Messages:
    221
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #6
    <select name="comments" onchange="document.getElementById('output').innerHTML=comments[this[this.selectedIndex].value];">

    should be:

    <select name="comments" onchange="document.getElementById('output').innerHTML=comments[this.options[this.selectedIndex].value];">
     
    briansol, Apr 13, 2007 IP