Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can select from drop down menu and call javascript function

I have a drop down which has many options. I want that when I select any option then it calls a function through JavaScript.

the code which I used is here

<select name="aa" onchange="report(this.value)"> <--- this is function in .js
<option value="daily">daily</option>
<option value="monthly">monthly</option>
</select>

I want when I select daily then function(daily) is invoked and vice versa.

function report(daily)<-- js function {  
  loadXMLDoc('script/d_report.php','responseTag');
  document.getElementById('responseTag').style.visibility='visible';
  document.getElementById('list_report').style.visibility='hidden';
  document.getElementById('formTag').style.visibility='hidden'; 
} 
function report(monthly) {
  document.getElementById('responseTag').style.visibility='visible';
  loadXMLDoc('script/m_report.php','responseTag');
  document.getElementById('list_report').style.visibility='hidden';
  document.getElementById('formTag').style.visibility='hidden';
}
like image 884
sadi Avatar asked Mar 16 '11 09:03

sadi


People also ask

What do you call the options for a drop-down menu?

A drop-down list (abbreviated drop-down, or DDL; also known as a drop-down menu, drop menu, pull-down list, picklist) is a graphical control element, similar to a list box, that allows the user to choose one value from a list.

What is drop-down menu in JavaScript?

JS Dropdown (dropdown. js) A dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list. For a tutorial about Dropdowns, read our Bootstrap Dropdowns Tutorial.

How do you create a dropdown list in JavaScript?

The <select> tab is used with <option> tab to create the simple dropdown list in HTML. After that JavaScript helps to perform operation with this list. Other than this, you can use the container tab <div> to create the dropdown list. Add the dropdown items and links inside it.


2 Answers

<select name="aa" onchange="report(this.value)">    <option value="">Please select</option>   <option value="daily">daily</option>   <option value="monthly">monthly</option> </select> 

using

function report(period) {   if (period=="") return; // please select - possibly you want something else here    const report = "script/"+((period == "daily")?"d":"m")+"_report.php";   loadXMLDoc(report,'responseTag');   document.getElementById('responseTag').style.visibility='visible';   document.getElementById('list_report').style.visibility='hidden';   document.getElementById('formTag').style.visibility='hidden';  }  

Unobtrusive version:

<select id="aa" name="aa">    <option value="">Please select</option>   <option value="daily">daily</option>   <option value="monthly">monthly</option> </select> 

using

window.addEventListener("load",function() {   document.getElementById("aa").addEventListener("change",function() {     const period = this.value;     if (period=="") return; // please select - possibly you want something else here      const report = "script/"+((period == "daily")?"d":"m")+"_report.php";     loadXMLDoc(report,'responseTag');     document.getElementById('responseTag').style.visibility='visible';     document.getElementById('list_report').style.visibility='hidden';     document.getElementById('formTag').style.visibility='hidden';    });  }); 

jQuery version - same select with ID

$(function() {   $("#aa").on("change",function() {     const period = this.value;     if (period=="") return; // please select - possibly you want something else here      var report = "script/"+((period == "daily")?"d":"m")+"_report.php";     loadXMLDoc(report,'responseTag');     $('#responseTag').show();     $('#list_report').hide();     $('#formTag').hide();    });  }); 
like image 101
mplungjan Avatar answered Oct 05 '22 23:10

mplungjan


Greetings if i get you right you need a JavaScript function that doing it

function report(v) {
//To Do
  switch(v) {
    case "daily":
      //Do something
      break;
    case "monthly":
      //Do somthing
      break;
    }
  }

Regards

like image 30
Marwan Avatar answered Oct 06 '22 00:10

Marwan