In my index.html, I have a selection drop down menu:
<select id="car">
<option value="bmw">BMW</option>
<option value="toyota">TOYOTA</option>
</select>
My js:
var c = $('#car');
var selection;
c.change(function(){
if(c.val()==='bmw'){
selection = 'BMW';
}else if(c.val()==='toyota'){
selection = 'TOYOTA';
}
});
console.log(c.val());
console.log(selection);
When page firstly loaded, the initial selection is BMW, and I got console output "BMW" which is fine, then I select "TOYOTA", but the two console outputs are still "BMW".
How to get the current
value of the selection outside jQuery .change(...)
function after the selection has been changed??
_________typo have been fixed_______
car.val()
and Selection
are both my typo here in the post, in my code I do use selection
, and c.val()
.
My point is how to get the current selection value out side jQuery change() function. Please do not discuss on my typo any more. Thank you.