0

I'm on a site where I would like to grab all the href links and click it. I know I could do this. document.getElementById('yourLinkID').click(); how ever, the issue is the href dosen't have an id, only a title. Can I somehow click all the href by it's title?

2

5 Answers 5

1

Accessing the document.links array would be the solution you are looking for.

From there, though, clicking one would cause the page to navigate away to its target, and the script would stop executing. If you must click them all, what you could do is loop through them, and set the target of an iframe with the link's href attribute.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use document.querySelectorAll() with selector a[href] to retrieve all <a> elements having href attribute set, or [href] to retrieve all elements having an href attribute set; for..of loop to iterate collection

var hrefs = document.querySelectorAll("a[href]");
for (let elem of hrefs) {
  // do stuff
  console.log(elem.href);
}

1 Comment

Thanks, I noticed I could just do this. var hrefs = document.querySelectorAll('[title=title_name"]'); How ever, it will only click the first thing than break. But that should not be hard to solve. Cheers!
0

use querySelectorAll:

document.querySelectorAll("[title=foo]")

Which will give you an array. iterate through the array if your goal is to click all the links. obviously, clicking a link will redirect you to that page and will pause script execution.

a dirty solution would be to use:

selectedElement.setAttribute('target', '_blank');

where selectedElement is the link's selector. this makes the url open in a new tab.

Comments

0

No idea why you would want to do this as each <a href... will be activated and after the first one it will be up to the browser to work out what happens.

var aList = document.getElementsByTagName('a');
var i, max = aList.length;
for(i=0;i<max;i++) {
    aList[i].click();
}
<a href="#" onclick="console.log('a1');return false">Hello</a>
<a href="#" onclick="console.log('a2');return false">world</a>

Comments

0

You can try the following solution,This will loop through each href and Click it.

$('a[href]').each(function ()
{
  $(this).trigger('click');
});

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.