try below thing buddy u missed closing bracket
$(function () {
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
});
});
Question
I am trying to learn how to use the change function in jquery but I cannot seem to trigger a function. Since I'm not the best at this, I figured i'd try someone else's code but that isn't working either. I copied the code from the first answer on this question
jquery not getting value from select statement on change
Html
<select id="drop1">
<option value="0">All</option>
<option value="1">Alphabetical</option>
<option value="2">Brewery</option>
<option value="3">Style</option>
</select>
JQuery
$(function () {
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
}
});
but I still can't get it to work. Here is the link i'm using for Jquery ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
Solution 5
try below thing buddy u missed closing bracket
$(function () {
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
});
});
OTHER TIPS
You have typos
$(function () {
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
});
});
Change in your code
$(function () {
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
} //remover this extra }
});
// add extra }); for closing DOM Ready function
Instead of $(this).val();
you can use this.value
should be like this
$('#drop1').change(function(e) {
var choice = $(this).val();
alert(choice);
});
you forget to enclose the .change() event properly.
here is your working fiddle http://jsfiddle.net/jogesh_pi/jCLJ9/2/
You didn't close the function call to change
so you're likely getting syntax errors in your JavaScript console. Note your version:
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
}
The closing }
ends the anonymous function
, but the parentheses for the change()
call are still open. You need to close them:
$('#drop1').change(function () {
var choice = $(this).val();
alert(choice);
});
Missing closing braces causing the error:
$(function(){
$('#drop1').change(function() {
var choice = $(this).val();
alert(choice);
});
});