Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Returning Value From Trigger

I am using a trigger call a get a value from custom event but I want it to return a value and its only giving me Object Object when I do the following call:

var user_id=$("#my_div").trigger("get_id", [username]); 

My trigger event function looks like this:

$("#my_div").on("get_id", function(e, username){     var user_id;     if (username='fred'){         user_id=1;     }     else if(username='mario'){         user_id=2;     }     return user_id; }); 
like image 644
Petty_Crim Avatar asked Feb 04 '12 22:02

Petty_Crim


People also ask

Can trigger return a Value?

The return value of a row-level trigger fired AFTER or a statement-level trigger fired BEFORE or AFTER is always ignored; it might as well be null. However, any of these types of triggers might still abort the entire operation by raising an error. Example 43.3 shows an example of a trigger function in PL/pgSQL.

What does trigger do in jQuery?

The 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 to trigger form in jQuery?

jQuery submit() Method The submit event occurs when a form is submitted. This event can only be used on <form> elements. The submit() method triggers the submit event, or attaches a function to run when a submit event occurs.

What is trigger click in jQuery?

trigger( "click" ); As of jQuery 1.3, . trigger() ed events bubble up the DOM tree; an event handler can stop the bubbling by returning false from the handler or calling the . stopPropagation() method on the event object passed into the event.


2 Answers

You cannot return a value from a trigger, but you can store information in many different ways, one way is using a object as a parameter:

//event $("element").on("click", function(event, informationObj) {        informationObj.userId = 2; //you have to access a propery so you can modify the original object }); //trigger var informationObj = {userId : 0}; $("element").trigger("click", [informationObj ]); //informationObj.userId === 2 

other way is using jQuerys .data() method

//event $("element").on("click", function() {      $(this).data("userId", 2);  }); //trigger $("element").trigger("click").data("userId") //2 

Another thing you can do is modifying a variable that's declared outside the event and then using it after calling the trigger, or storing it as a property in the element that has the event with the this keyword like this:

//inside the event function this.userId = 2;  //outside the event $("element").trigger("click").get(0).userId 

Hope it helps.

Edit:

Also, take a look at @Arm0geddon answer below, using .triggerHandler(), just beware that it has some side effects, like not bubbling up the DOM hierarchy.

like image 141
nicosantangelo Avatar answered Sep 20 '22 15:09

nicosantangelo


What you can do is use .triggerHandler() instead of .trigger(). This will return a value.

var user_id=$("#my_div").triggerHandler("get_id", [username]); 
like image 45
Arm0geddon Avatar answered Sep 20 '22 15:09

Arm0geddon