I'm trying to figure out the correct syntax for calling a Javascript function when a certain option is selected in an HTML drop-down menu.
This is the HTML segment of my menu:
<select id="change_chart" onChange="drawStuff()">
<option value="1" selected>livejournal</option>
<option value="2">librarybooks</option>
<option value="3">sunspots</option>
</select>
And this is my Javascript:
function drawStuff() {
var menu = document.getElementById("change_chart");
var selected = menu.addEventListener("change", generateData);
function generateData(event){
if (menu.value == '1') {
//do something
}
else if (menu.value == '2') {
//do something
}
else if (menu.value == '3') {
//do something
}
};
};
I'm new to event-driven programming so I have no idea if any of this is right. Anyone have any ideas?
drawStufffunction runs. So each time you change the input, you'll register a new event listener. This will slow down your site if someone were to keep changing the option.