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>
jQuery trigger() MethodThe trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
onchange: It is triggered when an HTML element changes. onclick: It is triggered when an HTML element is clicked. onmouseover: It is triggered when the mouse is moved over a HTML element. onmouseout: It is triggered when the mouse is moved out of a HTML element.
Go to the sources tab. Click on the Event Listener Breakpoints, on the right side. Then perform the activity that will trigger the event, i.e. click if the event that used is click, double click if it is dblclick event.
Update: I think I figured out what you want to achieve and this should work (there is no need in custom events):
function validate(element, 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);
}
}
$(document).ready(function(){
$("#txtFirstName").bind('focusout', function() {
validate(this, "defaultValue here", "Error message here", "Valid message here");
});
});
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:
$('#foo').bind('custom', function(event, param1, param2) {
alert(param1 + "\n" + param2);
});
$('#foo').trigger('custom', ['Custom', 'Event']);
and:
The event object is always passed as the first parameter to an event handler, but if additional parameters are specified during a
.trigger()
call as they are here, these parameters will be passed along to the handler as well.
So yours should look like this:
function(event, defaultInputValue, ErrorMessage, ValidMessage)
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:
$(document).ready(function(){
$("#txtFirstName").bind('focusout', function(){});
$(this).bind('customblurfocus',function(defaultInputValue,ErrorMessage,ValidMessage)
{ //...
});
});
To which element do you want to bind your custom event? What should $(this)
be? In this state, even with adding event
as first parameter to the function, this will not work.
Please tell us more about your HTML structure and/or what you want to achieve.
You can just make the call to your custom function within the body of the bound event handler:
$("#foo").bind("click", function(){
callMyFunction( arg0, arg1, arg2 );
});
E.g.
function customHandler(defaultInputValue,ErrorMessage,ValidMessage) {
// define the body of your custom function here
}
$(this).bind('customblurfocus', function(){
customHandler(defaultInputValue, ErrorMessage, ValidMessage);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With