简体   繁体   中英

Javascript drop down menu onchange event only for one fuction

This is what I am trying to achieve: within the function below, when the drop down menu changes, the function will get the value from a textbox and perform look up. I don't wanna add a onchange to the <select> tag, as there are other functions linked to the drop down menu. Can I add a on change only to this function below? Many thanks in advance!

        <select id="types"> 
            <option value ="food">food</option>
            <option value ="drinks">drinks</option>
        <select>
           function lookup(){
                    var keyword = input.value;//get value from text box
                    var type = document.getElementById("types").value;//get value from drop down menu
                    if (search=="food"){
                    //perform look up
                    //I 'm trying to make it do another search when the type from the drop down menu changes
                }else{

                }       
            } 

You can add an event handler like this

 window.addEventListener('change', function(e) { if (e.target.id == "types") { lookup(e.target); } }); function lookup(){ alert("here now..."); return ; // temp exit var keyword = input.value;//get value from text box var type = document.getElementById("types").value; //get value from drop down menu if (search=="food"){ //perform look up //I 'm trying to make it do another search when //the type from the drop down menu changes }else{ } } 
 <select id="types"> <option value ="food">food</option> <option value ="drinks">drinks</option> <select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM