Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use trigger in Jquery to pass parameter data as well as control (this)

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>
like image 574
Shantanu Gupta Avatar asked Apr 07 '10 22:04

Shantanu Gupta


People also ask

How to trigger function in jQuery?

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.

How do you trigger a function in Javascript?

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.

How do you check if an event has been triggered in Javascript?

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.


2 Answers

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.

like image 118
Felix Kling Avatar answered Oct 14 '22 15:10

Felix Kling


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);
});
like image 36
Mark Ursino Avatar answered Oct 14 '22 15:10

Mark Ursino