TurtleBo TurtleBo - 1 year ago 255
Javascript Question

Autoclick JavaScript link in Table

So I've got this HTML Table as follows:

<td><a href="link1"></td>
<td><a href=link2"></td>
<td><a href="link3"></td>
<td><a href=link4"></td>

It has many more rows than this but this is just a section for the purpose of my question. I know Javascript uses .click to click on links, but I am wondering if there is any way to select the link in the first row first column, because on different web pages the first link in the first column will be different than the one on this page. If this is possible how do I do so? Thank you so much. I looked into this trying to find the answer on my own and I didn't find one. If it helps, I am using Tampermonkey to run the script. Thank you for your help!


Answer Source

This will return you first link from the table

document.querySelector('table a')

if you want first link from the webpage then use


If you want to click on it then

document.querySelector('table a').click()

Or if you want to open in different window then,

window.open(document.querySelector('table a').href)

document.querySelector('#profile_creatures table a').click()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download