Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a jQuery Click method?

Tags:

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?

like image 965
jedd Avatar asked Dec 05 '08 13:12

jedd


People also ask

How use jQuery click method?

To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method. The user can attach a function to a click method whenever an event of a click occurs to run the function.

Is jQuery click deprecated?

click() shorthand is deprecated at jQuery 3 The . on() and . trigger() methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods.

What is difference between click and Onclick in jQuery?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.

Is click a method?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


Video Answer


1 Answers

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'); 
like image 188
Pim Jager Avatar answered Mar 06 '23 06:03

Pim Jager