Why this works in jQuery :
$('#selCars').change(function(){
alert( "I have changed!" );
})
but not this one :
$('#selCars').change(alert( "I have changed!" ) );
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You pass a function reference to
.change(). Your second example just has code there, not a function reference.Your first example works because it passes a function reference which IS what is required.
A function reference is required because this is a callback that will be called at a later time. The
.change()function which executes immediately needs to save the callback reference into it’s own variable and then call it later when the change event actually occurs. To do that, it needs a function to call at that later time, not a raw piece of code.And, the other answer is because,
.change()was written to require a function reference. That’s how the developers that spec’ed and wrote it designed it. If you want it to work, you have to follow their rules.