1

I have a very basic click-function which does some functions:

$('#one').click(function() { 
    slider();
    $(this).css({'background-image':'url(img/circle_c.png)'});
    $('#two, #three').css({'background-image':'url(img/circle.png)'});
    $content.cycle(0); 
    return false;  
});

now I want to trigger this click function at another click-function. I mean once I press another click-function which currently looks like this:

$mario.on('click', this, function() {
    var l = 0,
    row = 8;
    $(this).addClass('active');
    // and some other functions
});

the first click-function fires as well. Does anyone of you know how to do this. I guess copy and paste the content of #one clickfunction is not nessecary.

Cheers

3 Answers 3

6

You could use trigger

$('#one').trigger('click');

So your complete code would looks like this

$mario.on('click', this, function() {
    var l = 0,
    row = 8;
    $(this).addClass('active');
    $('#one').trigger('click'); // < -- trigger click for #one
    // and some other functions
});
Sign up to request clarification or add additional context in comments.

5 Comments

or $('#one').click();
@ClaudioRedi this works fine, but unfortunenately it does not trigger the $content.cycle() function in the #one click. Don't know why
@ClaudioRedi nope, nothing. Its cycle.js and all functions inside the triggered function do their job, but the cycle not.
@supersize: I don't see that $content is defined inside click handler, maybe someone else is messing up with that global variable?
@ClaudioRedi no, $content works at the first click-function. And as the trigger refers to that it should work. Its global and no issues. But I guess this isnt your point anymore, question is correctly answered.
0

Break the first click-handling code out into a function:

function swapImages() {
  slider();
  $('#one').css({'background-image':'url(img/circle_c.png)'});
  $('#two, #three').css({'background-image':'url(img/circle.png)'});
  $content.cycle(0); 
  return false;  
}

And reference it in both places:

$('#one').click( swapImages );

$mario.on('click', this, function() {
    var l = 0,
    row = 8;
    $(this).addClass('active');

    swapImages();

    return false;
});

Comments

0

You have two options. You can use the trigger method.

$("#one").trigger('click');

Or you can use the click method with no arguments. Example here.

$("#one").click();

Comments

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.