trikess
trikess

Reputation: 71

jquery trigger click on other element

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/

Upvotes: 0

Views: 68

Answers (3)

jandro5
jandro5

Reputation: 56

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

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

Upvotes: 2

jdmcnair
jdmcnair

Reputation: 1315

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();       
});

Upvotes: 0

Milind Anantwar
Milind Anantwar

Reputation: 82241

Try this:

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

Demo

Upvotes: 0

Related Questions