0

I had a look through others similar questions and I haven't got a clue why mine solution doesn't work. Maybe you can spot something I'm missing. What I'm trying to do is trigger a click while clicking on div with id badge

HTML

<div id="call-out">
    <div id="badge">Brochure</div>
    <p>Add <a href="http://www.google.com" target="_blank">test</a> content</p>
</div>

jQuery:

$('#badge').on('click', function(e) {
    e.stopImmediatePropagation();
    if (! $(e.target).is('a')) {
        $(this).parent('div').find("a").trigger('click');
    }
});

Any suggestions appreciated. Here is my test: http://jsfiddle.net/m89wa/2/

4
  • The click is working, as demonstrated here: jsfiddle.net/m89wa/4 It appears that the popup is not working. Commented May 22, 2014 at 14:35
  • I was assuming that it will take look for the href attribute and open it without doing window.location, maybe I was assuming wrongly. Commented May 22, 2014 at 14:39
  • Yeah, so if you're really looking for it to open a new window you can do a window.open("") or do what @jandro5 suggests below. Commented May 22, 2014 at 14:41
  • Thanks for quick response. I will go with window.open() and read the href attribute from <a> element: jsfiddle.net/m89wa/5 Commented May 22, 2014 at 14:49

3 Answers 3

2

change: $(this).parent('div').find("a").trigger('click');

try: location.href = $(this).parent('div').find("a").attr("href");

Sign up to request clarification or add additional context in comments.

1 Comment

Good answer - add window.open as an alternative and this is what @trikess is looking for.
0

Try this:

$('#badge').on('click', function(e) {
 e.stopImmediatePropagation();
 $(this).parent().find('a').trigger('click');
});

Demo

Comments

0

If you call the click function without an argument on a jquery element, it will trigger the click event on that element.

http://api.jquery.com/click/

$('#badge').on('click', function(e) {
    e.stopImmediatePropagation();
    $(this).parent().find('a').click();       
});

3 Comments

The only difference is .click() instead of .trigger('click'). Not much difference, but it is a bit more succinct.
This does not answer the original question - he was looking to have it behave as if the <a> was clicked. Programmatically invoking click does not open the new window, which is the OP problem.
It actually does answer the original question, namely how to trigger a click event from another element. The extra requirement to open a separate window is something that you helped to sniff out in the comments after I'd given this answer.

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.