Reputation: 11355
So I have a list of option values that, when they are selected, I want to pass information to Javascript, so that I can try to have Javascript contact the server and update several other fields based on that option value (AJAX - something I'm new to!)
How do I let Javascript know that I selected a particular option value?
Upvotes: 1
Views: 75
Reputation: 3948
add for each selectboxes the event onChange:
<select name="{any name}" onChange="myVJsValuecheckFunction(this)">
<option value="{myOptionValueOne}">myoptionText</option>
...
</select>
Then write the javascript function:
function
Upvotes: 0
Reputation: 360922
In ugly direct-coding methods:
<select onchange="the_select_was_changed(this);">
<option>...</option>
<option>...</option>
</select>
function the_select_was_changed(obj) {
alert("The selected index is " + obj.selectedIndex + " and has value " + obj.options[obj.selectedIndex]);
}
Upvotes: 0
Reputation: 166071
You can bind a change event listener to the select
element (this assumes your select
element has an id
of yourSelect
):
document.getElementById("yourSelect").onchange = function() {
var selected = this.value;
//Do whatever with the selected value
}
Here's a working example. The onchange
event listener is called whenever the value of the select
changes. You can access the value
of the selected option with this.value
, and pass that to your server side script.
Upvotes: 7