tags:

views:

23406

answers:

8

I'm trying to click on a link using jquery. There only appears to be a click event that replicates "onclick" (i.e user input). Is it possible to use jquery to actually click a link?

+4  A: 

$(-some object-).trigger('click') should do the trick.

kamal.gs
Documentation and code sample is here http://docs.jquery.com/Events/trigger
Alexander Prokofyev
Works, but you might as well just do $(object).click()
TM
+8  A: 

I prefer $(-some object-).click() for readability

If you pass the click() method a function, it behaves like an onClick event binding:

i.e. $(-some object-).click(function() { -do stuff- })

Brabster
+20  A: 

From your answer:

$("a[0]")

is not a valid selector. to get the first a on the page use:

$("a:first")

or

$("a").eq(0).

So for the selector in your answer:

$("table[1]/tr[1]/td[1]/a").trigger('click');

write

$("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').click();

Note how this will click all the links in the second table cell of the second table row in the second table on your page.
If you use this method to redirect the page to the href of the a the following method is slightly nicer:

document.location = $("table").eq(1).children("tr").eq(1).children('td').eq(1).children('a').attr('href');

Note how this will set the document location to the href of the first a found in the second table cell of the second table row found in the second table on the page.
If you want to match the first elements use eq(0) instead of eq(1).

EDIT
If you really want to do this 1337-haxxor

$("table:eq(1) > tr:eq(1) > td:eq(1) > a").click();

however I think the other method is more readible.

EDIT

Okay, from you next answer/question thingie
How about not actually clicking on the link but just setting the document.location string to it:

document.location = $("table").eq(0).children("tr").eq(0).children('td').eq(0).children('a').eq(0).attr('href');
Pim Jager
Thanks Pim. I was trying to replicate user behavior, but that is a good workaround.Many thanks.
jedd
If this is the answer your going to use you should accept it.
Pim Jager
Changing "document.location" has just one flaw: it breaks the back button...
BlaM
It also does not populate HTTP Referrer.
Chase Seibert
A: 

This is still not working!!

The xpath plugin is not very good. Very limited xpath support.

I have my expression corrrect now :

alert( $("table tr:eq(0) td:eq(0) a:eq(0)").length); *which equals one *

but when I try to click on the link :

$("table tr:eq(0) td:eq(0) a:eq(0)").trigger('click');

no joy :(

jedd
Maybe clicking on the link is not something allowed, see my updated answer.
Pim Jager
Pim,What do you mean by not allowed (Not implemented in jquery)? You example is replicating 'onclick' event of javascript. I want to replicate 'click' (See original post). Perhaps I can use the jquery object and the javascript click method.
jedd
This is not an answer.
Herb Caudill
A: 

Try it this way:

$("table:first").find("tr:first").find("td:first").find("a:first").click();

That will trigger the onclick event of the the first a in the first cell of the first row in the first table...and its very readable in itself.

Andreas Grech
I don't want to trigger the onclick event!!!!!I want to programtically click on the object!!!Javascript equivalent :window.document.getElementsByTagName("table")[0].rows[0].cells[1].children[0].click();
jedd
"programtically click on the object"what does that mean if not trigger the onclick event of the retrieved element ? you want to assign an onclick function, or ?
Andreas Grech
+2  A: 
$("table:first").find("tr:first").find("td:first").find("a:first")[0].click();

This will work in Internet Explorer if thats your only target, otherwise you're stucked with the document.location solution.

bang
A: 

This works successfully:

    $(document).ready(function() {
        $("#horizontalSlideButton").trigger('click');
    });

Where horizontalSlideButton is the ID of the link you want to trigger the click event for.

As soon as the DOM is loaded, the contents of $(document).ready(function() {} are loaded.

Please mark if this helps!

ElHaix
A: 

I had a similar problem and found that by creating a fake event object and then dispatching it using dispatchEvent() replicated a user click very nicely:

var event = document.createEvent("HTMLEvents");
event.initEvent("click", true, true);
document.getElementById('myID').dispatchEvent(event); 

Obviously this is not using JQuery, but it might help in your task.

Bojanglez