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?
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- })
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');
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 :(
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.
$("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.
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!
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.