I've got a fiddle for you: http://jsfiddle.net/pneebntd/3/
$(document).ready(function(){
$('#Address').focusout(ValidateAddress($(this).val(), "address"));
$('#City').focusout(ValidateAddress($(this).val(), "city"));
$('#State').focusout(ValidateAddress($(this).val(), "state"));
$('#Zipcode').focusout(ValidateAddress($(this).val(), "zip/postal code"));
$("#StateList").change(ValidateAddress($(this).val(), "state"));
});
function ValidateAddress(location, label) {
console.info("made it there : " + location + " " + label);
}
The short of it is that I'm (trying) to attach the event handler for a function I want to run when a control loses its focus (or when a dropdown changes value).
The way it's written, it fires on page load but never again after that. I've done this before but maybe it's just because it's Monday but... what am I doing wrong here?
This code
$('#Address').focusout(ValidateAddress($(this).val(), "address"));
calls ValidateAddress
and passes its return value into focusout
, exactly the way foo(bar())
calls bar
and passes its return value into foo
.
If you want to give a function to focusout
, you have to do that instead. For instance:
$('#Address').focusout(function() {
ValidateAddress($(this).val(), "address");
});
That code creates a new anonymous function and passes that function reference into focusout
. When the event occurs, it will call your ValidateAddress
function.