1

I have a case where a click handler is defined/assigned in one jQuery code block (file) and I want to trigger it from another click event defined/assigned in a different jQuery code block. How can I accomplish this?

The following code is a greatly simplified version of what I am trying to accomplish. The behavior I want to see is a JavaScript alert "Element One" when I click #Element2.

Example HTML:

<p id="Element1">Element One</p>
<p id="Element2">Element Two</p>

First jQuery code block:

$(document).ready(function() {
    $('#Element1').click(function() {
        alert('Element One');
    });
});

Second jQuery code block:

$(document).ready(function() {
    $('#Element2').click(function() {
        $('#Element1').click();
    });
});

UPDATE: My original example actually works. I was building upon my field hint jQuery UI Dialog solution, and didn't account for about the 'clickoutside' handler that I was using. Adding a check to for the second element in my 'clickoutside' handler allows the dialog to display.

2 Answers 2

2

You need to trigger a click when you click on the first element. You can use the trigger method for this.

function element1Hanlder () {
    alert('Element One');
}

$(document).ready(function() {
    $('#Element1').click(function() {
        alert('Element One');
    });
});

$(document).ready(function() {
    $('#Element2').click(function() {
        $('#Element1').trigger('click');
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

You're assigning the handler to #Element1 a second time when #Elment2 is clicked which is not what I am trying to accomplish. I want to trigger the handler that was already assigned.
@Sonny I read it the other way around. What you need is trigger. Updated
It's still not working for me. I assume it's a scoping issue because the handler is defined inside the first anonymous $(document).ready(function() {...}); call, and is not visible in the second anonymous call.
@sonny the scope shouldn't matter here. You're simply triggering a click on the element. Do you have any other event handlers on it? Here's an example : jsfiddle.net/6VHeP
It turns out there was a different issue. Even my example code works as written.
1

EDIT: This is based on JohnP's "trigger" suggestion (so you should choose him as the right answer)...

If I load this block from an external js file...

$(document).ready(function() {

    $('#Element1').click(function () {
        alert( $(this).text() );
    });

});

Then load this in a script tag within the HTML itself...

$(document).ready(function() {

    $('#Element2').click(function () {
        $('#Element1').trigger('click');
    });

});

Seems to be working as intended.

3 Comments

I'm thinking my reply is too simplified and JohnP's 'trigger' suggestion is the more accurate solution.
This needs to be two separate $(document).ready(function() {}); code blocks.
@Sonny please see my edits. Tested this out and seems to be working.

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.