0

I am trying to pass a data from one click function to another and have no idea how to make it. I have multiple divs but i will show you the main idea on two divs:

<div class="classname" id='prev'></div>
<div class="classname" id='next'></div>


$('#next).click(function() { 
   $('.classname').trigger('click')

})

$('#prev).click(function() { 
   $('.classname').trigger('click')

})


$('.classname').live('click', function () {
 [a lot of code here depending on who called click]


})

now, i would like to pass a variable like that (or somehow familiar

$('#prev').click(function() { 
   a = 1;
   $('.classname').trigger('click', a)

})


$('.classname').live('click', function (a) {
 if ( a == 1 ) { do something } else { do something else }
 [a lot of code here depending on who called click]

})

How can i send 'a' variable to live click to let it baheva differently depending on value of 'a' variable?

2
  • You're going to get huge circular references with this code. clicking #next or #prev trigger's a click on the same element, which triggers a click on the same element, which..... StackOverflow Commented Aug 1, 2012 at 16:01
  • Maybe you could explain what you are trying accomplish? You can definitely pass parameters along and trigger click events on a set of elements ... but the real question is if there is simply a better/cleaner approach. But we need to understand your goal first. Commented Aug 1, 2012 at 16:11

4 Answers 4

1

I think you're looking for this (see DEMO):

$('#prev').click(function () {
    $('.classname').trigger('click', [1])
});

$('.classname').live('click', function (event, param) {
    if (param == 1) {
        alert('do something');
    } else {
        alert('do something else');
    }
})​
Sign up to request clarification or add additional context in comments.

Comments

0

That is already possible! Sample code from: http://api.jquery.com/trigger/

$("p").click( function (event, a, b) {
// when a normal click fires, a and b are undefined
// for a trigger like below a refers to "foo" and b refers to "bar"

} ).trigger("click", ["foo", "bar"]);

You can also create a custom event object and send some data in it:

var event = jQuery.Event("logged");
event.user = "foo";
event.pass = "bar";
$("body").trigger(event);

Or you can use this alternative:

$("body").trigger({
    type:"logged",
    user:"foo",
    pass:"bar"
});

Comments

0

You were very close, the extra arguments passed to an event need to be expressed as an array.

$('.classname').trigger('click', a)

should have been

$('.classname').trigger('click', [a])

Which can then be accessed as additional arguments on the handler for that event

$('.classname').live('click',function(e,a){
    // a is whatever was passed into the first element of the array above
})

Live example: http://jsfiddle.net/4KRFV/

Comments

0

Sorry, but I think you are doing this in a overcomplicated way. Call a common function from the two event handlers. Use different arguments depending from which event handler you call it. Stay away from using .trigger and .live.

2 Comments

Please, elaborate on that! Calling one event handler from another cant be a good practice really, especially not in a difficult event oriented application.
Oh, I think you formulated yourself in an understandable way. I will now tell you mine (and entirely mine, lol) point of view. Calling an event handler from your own code can be substitution of concepts. The original concept of an event being "a reaction from the other world". Calling a public function from another public function in the same class is a substitution. The original concept being "providing an interface to the outer world". If event handlers/public functions only are "receiving points" calling the actual functionality, code duplication would be of minimal concern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.