Javascript calling from dropdown box

Discussion in 'JavaScript' started by bananalive, Aug 4, 2008.

  1. #1
    I have php displaying a dropdown box with list of rows in mysql database. On dropdown select of an option I want a javascript variable to be define with the value in the mysql row selected. Is this possible in one query?

    Thanks
     
    bananalive, Aug 4, 2008 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    something like this ...

    
    <html>
    
    <head>
    <script type="text/javascript">
    function assignVar()
    {
      var myObj = document.getElementById("mySelect");
      myVar = myObj.options[myObj.selectedIndex].text;
      // do whatever  ...
      alert(myVar);
    }
    
    </script>
    
    </head>
    
    <body>
    
    <form method="POST" action="">
      <select id="mySelect" size="1" name="mySelect" onchange="assignVar()">
      <option>choose ...</option>
      <option>this</option>
      <option>that</option>
      <option>then</option>
      <option>there</option>
      </select>
    </form>
    
    </body>
    
    </html> 
    
    Code (markup):
     
    mallorcahp, Aug 5, 2008 IP
  3. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you can use "this.value" when you are calling the function as selected value

    
    <script language="Javascript">
    function assignVar(selected)
    {
    doSomething()
    }
    </script>
    
    HTML:
    
      <select id="mySelect" size="1" name="mySelect" onchange="assignVar(this.value)">
      <option>choose ...</option>
      <option>this</option>
      <option>that</option>
      <option>then</option>
      <option>there</option>
      </select>
    
    
    HTML:
     
    yleiko, Aug 5, 2008 IP