tags:

views:

132

answers:

2

So the page is broken down some sections and each section has a 'a' tag. When clicking on the link it fires the 'onclick' event which calls a javascript function.

I am adding a link at the top of the page that when you click on the link it will call a JQuery function .click that will call all the 'a' tag 'onclick' events.

Is this possible?

The other coveat is that in the 'onclick' event, the function that is called is passing the ID of the parent DIV id. So I really need to fire the 'onclick' event and not call the javascript function directly within the JQuery function.

+5  A: 

use trigger():

$("a#mylink").trigger("click");

Or the shortcut, click():

$("a#mylink").click();
Andy E
+2  A: 

You can use .click() directly, like this:

$("a.selector").click();

Give it a try here, though it's a jQuery function, it'll call the in-line bound click handlers as well.

Nick Craver
This isn't working. To give me info: I am calling the .each for all the 'a' tags in the main DIV - example:$("mainDiv a').each(function() { $('a').click();and No luck.
pghtech
@pghtech - You should use `$(this).click()` there, or unless you need the `.each()`, just `$("mainDiv a').click()`. Also, which version of jQuery are you on? 1.4.2 got a major events module re-write.
Nick Craver
I am not sure atm. I have tried $(this).click, and I have even tried $('#mainDiv a').click() as well. Neither of them will fire the 'onclick' event on each of the 'a' tags. For clarification, does the $('#mainDiv a').click() mean it would do it for each a tag in that DIV, and you don't have to put the line inside a .each(function()?any other thoughts?
pghtech
@pghtech - Correct it would click each `<a>` with that code...do you have a sample page I could take a look at to see the issue?
Nick Craver
Thanks for the help and explanation. I figured out the problem. It had to do with the logic and parameters in the function that was ultimately being called.
pghtech