I have a drop-down menu in my page, it’s something like this:
<select id="my_dropdown" name="my_dropdown">
<option value="one">ONE</option>
<option value="two">TWO</option>
<option value="three">THREE</option>
</select>
<input type="text" size="10" id="foobar" value="0" />
And my jQuery code is:
$('#foobar').keyup(function(){
notOptionOne();
}
function notOptionOne() {
if ( !$('#my_dropdown').val('one') ) {
alert('Option ONE was not selected');
}
}
No matter what is the selection on my dropdown, when I enter any text on the input box, it will show up the alert box and will reset my dropdown selection back to one. I want to keep my selection on where I leave when I close the alert box or do anything else. What is causing this behaviour?
The val() function in jQuery can both get a value or set a value. when you pass a parameter to it, what jQuery do is to set the value of the control. If no parameter, get value.