How to call function in SELECT button using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • askrk
    New Member
    • Apr 2010
    • 1

    How to call function in SELECT button using javascript

    1.im using php programming.how to call a function using select button in form but no output has been shown........
    2.script page
    Code:
    <script type="text/javascript">
    function education_onchange()
    {
    alert("education onchange");
    }
    function personal_onchange()
    {
    alert("personal onchange");
    }
    </script>
    3.body page..

    Code:
    <form name="form" id="form_job_seeker_home" action="" method="post">
    <table border="1">
    <tr>
    <td>
    <select >
    <option selected="selected">-select the option-</option>
    <option value="education" onchange="education_onchange();" id="education" >education</option>
    <option value="personal" onchange="personal_onchange();" id="personal">personal</option>
    </select>
    </td>
    </tr>
    </table>
    </form>
    </body>
    Last edited by Dormilich; Apr 6 '10, 11:58 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you don’t call it on the <option> (because the option element doesn’t change) but on the select element.
    Code:
    <select id="test">
        <option selected="selected">-select the option-</option>
        <option value="education" id="education" >education</option>
        <option value="personal" id="personal">personal</option>
    </select>
    
    function alertChange()
    {
        alert(this.value);
    }
    document.getElementById("test").addEventListener("change", alertChange, false);
    works in all modern browsers

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      in this case you would need to handle the onclick events ... otherwise you would need to assign the onchange handler to the select node not to the option-nodes ...

      kind regards

      PS: nearly posted the same time :) ...

      Comment

      Working...