6

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?

Community
  • 1
  • 1
asfsadf
  • 3,822
  • 7
  • 31
  • 41

2 Answers2

7

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();
});
Community
  • 1
  • 1
cambraca
  • 27,014
  • 16
  • 68
  • 99
  • seems like one of the answers to that question actually does show a cross-browser way to do this! See the answer referencing Selenium... – Ryley Oct 28 '10 at 23:09
  • @Ryley well that looks promising! but I'm not gonna test it in every browser right now :) – cambraca Oct 28 '10 at 23:12
  • There's another question about this subject [here](http://stackoverflow.com/questions/1421584/how-can-i-simulate-a-click-to-an-anchor-tag) – cambraca Oct 28 '10 at 23:14
  • This one does the trick. Thanks. – asfsadf Oct 28 '10 at 23:40
4

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();
});
code90
  • 718
  • 9
  • 22