25

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?

1
  • 3
    Do you mean to programmatically emulate the click on a link? Commented Dec 5, 2008 at 13:25

8 Answers 8

35

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');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Pim. I was trying to replicate user behavior, but that is a good workaround. Many thanks.
If this is the answer your going to use you should accept it.
It also does not populate HTTP Referrer.
8

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- })

Comments

4

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

1 Comment

Works, but you might as well just do $(object).click()
4

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!

Comments

3

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.

Comments

2
$("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.

Comments

0

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.

2 Comments

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();
"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 ?
0

I prefer to use the live function such as (for a class)

  $(document).ready(function() {
    $(".myclass").live('click', function(){//do something});

});

or for an id

 $(document).ready(function() {
    $("#myid").live('click', function(){//do something});

});

I use a span class to style my link and give it a class or id like

    <span class="link"><a href="javascript:void(0);" target="_new">my link/a></span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.