Search code examples
javascriptcss-selectorstarget

JS select all anchor elements inside a table


I have an html page with a section with a table. It consist of links and i need to get them all, preferably in some sort of an array. When I try to target a single link in the table, it works, i just copy the JS path and it works. But i cant target all the links in the table no matter what I do, it often returns null or gives an error. Here is the path to a single rown in a table, this works, but i need all the a hrefs inside the table, pls help!

#anchor-text-chart-section > div.table-container.mob-single-panel > table > tbody > tr:nth-child(3) > td.anchorText > a

Solution

  • const elements = document.querySelectorAll('#anchor-text-chart-section table a');
    const urls = [];
    for (const element of elements) {
      urls.push(element.href);
    }
    console.log(urls);