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.
You should define your var selection with lowercase, as this:
Why? Javascript is case sensitive, so, Sensitive and sensitive are distinct variables.
Also, you should define your car var, like this:
You're looking at
car
when you should be usingc
.car
does not have aval()
method.Also note that
var Selection;
will not be the same variable asselection
(lowercase).A better approach might be:
working example: http://jsfiddle.net/hunter/XespU/