Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - manually trigger link inside of a <div> that gets clicked on [duplicate]

Tags:

jquery

Possible Duplicate:
How can I simulate an anchor click via jquery?

For some reason I thought this would be really easy. Here's my code:

$('#someDamnedDiv').live('click', function () {
    $('a', this).trigger('click');
});

What the Sam Hill is wrong with my function?

like image 639
asfsadf Avatar asked Oct 28 '10 22:10

asfsadf


2 Answers

I don't think it's possible to simulate a click on a link. Look here. However, you could do this:

$('#someDamnedDiv').live('click', function (event) {
    window.location = $(this).find('a').attr('href');
    event.preventDefault();
});
like image 83
cambraca Avatar answered Oct 22 '22 10:10

cambraca


this is what i use for one of my projects:

$('div.classname').click(function(e){
    if($(e.target).is('a')) return;
    $(this).find('a').click();
});
like image 43
code90 Avatar answered Oct 22 '22 12:10

code90