Reputation: 436
I have select box as
<select id='selectboxID'>
<option value="">select id</option>
<option value="Actor">Actor</option>
<option value="Child artist">Child artist</option>
<option value="">Musician</option>
</select>
My jquery code
$(function() {
var option = 80;
$("#selectboxID").change(function() {
option = 17;
});
alert(option);
})
My requirement is When I select any option then option variable value should be 17 .Please advise how to do.
Upvotes: 0
Views: 1462
Reputation: 1289
Move the global variable outside of the function
var option = 80;
$(function() {
$("#selectboxID").change(function() {
option = 17;
alert(option);
});
})
Upvotes: 1
Reputation: 74738
Put an alert inside change event:
$(function() {
var option = 80;
$("#selectboxID").change(function() {
option = 17;
}).change(); //<---------change like this
alert(option);
});
Upvotes: 0
Reputation: 73906
Your scope of the alert and the option
value checking is not correct, try like this:
$(function () {
// Check option value on load
var option = 80;
alert(option);
$("#selectboxID").change(function () {
option = 17;
// check the option on dropdown onchange event
alert(option);
});
});
Upvotes: 0
Reputation: 3610
Declare it outside the function and inside the script tag
<script type="text/javascript" language="javascript">
var option=80;//Global Declaration
$(function() {
$("#selectboxID").change(function() {
option = 17;
});
alert(option);
});
</script>
Next time when u will call the function you will get the previous value assigned to option
variable
Upvotes: 0
Reputation: 30115
The $(function() { })
statement says that you hare writing handler of the document.ready
event. So all variables declared inside of this block will be visible only inside of this block. You just need to move out of your block your variable declaration:
var option;
$(function() {
option = 80;
$("#selectboxID").change(function() {
option = 17;
});
alert(option);
})
Upvotes: 0