Reputation: 15459
when clicking this select element, the inline call "onchange" should call a javascript function:
<select onchange="if (this.selectedIndex) do_something();">
<option value=1>1</option>
<option value=2>2</option>
</select>
basically how do i submit the option selected to 'do_something()' function? or in the function how can I obtain the option value selected?
Upvotes: 2
Views: 3273
Reputation: 61
function do_something(i){
console.log(i);
}
<select onchange="do_something(this.selectedIndex);">
<option value=1>1</option>
<option value=2>2</option>
</select>
Try adding it as a parameter in the do_something function.
Upvotes: 1
Reputation: 272608
you may try this :
function do_something(e) {
alert(e);
}
<select onchange="if (this.selectedIndex) do_something(this.value);">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
You may notice that i won't work for 1 because of selectedIndex which is equal to 0 for the first index (the value 1)
so more generic it can be :
function do_something(e) {
//we make do test on the element e
alert(e.value);
}
<select onchange="do_something(this);">
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
Upvotes: 2