1

Ok, so this is a bit complicated. I have something like this code:

<div> Hello, I'm inside a DIV, please <a href='foo'>click here</a></div>

And I want to bind a click event to the DIV that follows the link of the contained A. No worries, this I can do all by myself, but the only method I know of is using "top.location", which bypasses the browsers normal handling of the link

In a nutshell, if I do it this way, if I hold my alt-key pressed when I click the DIV, the link won't open in a new tab, which is the behaviour in my browser.

Any ideas what so ever to handle this? I can't pre-code this new tab/new window behaviour since its user-configurable.

Sandman
  • 2,323
  • 5
  • 28
  • 34

2 Answers2

1

Try this: $("a").trigger("click");

To open it in a new tab, you could set an attribute target="_blank" before triggering the event.

kjagiello
  • 8,269
  • 2
  • 31
  • 47
  • Ok, I get "maximum call stack size exceeded" when I do that. See my example code here: http://sandman.net/test/hover_links.html – Sandman Jan 14 '10 at 14:26
  • `$("li.list a").click(function(){ event.stopPropagation() });` seems to cause the problem `event is not defined: event.stopPropagation()` – kjagiello Jan 14 '10 at 14:48
  • Your code to stop event propagation is needed. I've corrected it. `$("li.list a").click(function(event){ event.stopPropagation(); });` – kjagiello Jan 14 '10 at 14:54
  • Right, thanks - that removed the errors I got too. But the part of my code that does the trigger() part still doesn't work. Like 20 in my code is: $(this).find("a.mainlink").trigger("click"); Upon clicking the LI, it does nothing... – Sandman Jan 14 '10 at 14:57
  • Take a look at http://stackoverflow.com/questions/143747/is-it-possible-to-trigger-a-links-or-any-elements-click-event-through-javascr – kjagiello Jan 14 '10 at 15:33
0

It isn't possible to trigger the browser's default handling of a link. The click() method (or jQuery equivalent) only runs the event handlers associated with a link; it does not follow the link, much less allow shift-click, middle-click, right-click-bookmark and so on.

To get the full behaviour of a link, you will need a real link. Wrap the contents of the following <div> in a new <a> with href pointing to the same place. Then if you need to, style the link so that it doesn't look like a link.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Right, but the problem with that solution is that it's not "legal" to nest links, and I really want to be able to have other links within the DIV, as you can see in my example link... – Sandman Jan 14 '10 at 15:25