I am trying to use same validation function for all my controls. But I don’t know much in jQuery and not been able to pass event handler to the trigger. I want to pass textbox id to my custom function.
How can I do this
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#txtFirstName").bind('focusout', function()
{
$(this).trigger("customblurfocus",[$(this).val(), $(this).val() + " Required !", "Ok !"]);
}
);
$(this).bind('customblurfocus',function(defaultInputValue,ErrorMessage,ValidMessage)
{alert($(this).val());
if($(this).val()=="" || $(this).val()==defaultInputValue)
{
$(this).val(defaultInputValue);
$(this).siblings("span[id*='error']").toggleClass("error_set").text(ErrorMessage).fadeOut(3000);
}
else
{
$(this).siblings("span[id*='error']").toggleClass("error_ok").text(ValidMessage).show(1000);
}
}
);
});
</script>
Update: I think I figured out what you want to achieve and this should work (there is no need in custom events):
If you have questions, just ask 🙂
The first parameter to your bound event handler is the event object itself. See the example from the
.trigger()documentation:and:
So yours should look like this:
But it would be better if you described what error you do actually get.
Second issue: If I see correctly the second bind is somehow without context:
To which element do you want to bind your custom event? What should
$(this)be? In this state, even with addingeventas first parameter to the function, this will not work.Please tell us more about your HTML structure and/or what you want to achieve.