1

I know that I can return an array like so:

var links = document.links.

I could then loop through the array for where the href attribute = /somehref

My question is is there some nifty shortcut when creating links var? Something to the effect of:

var links = document.links[href='/somelink']

Thus negating the need for a for loop?

Elements with href='/somelink' can be both <a> elements and <button> elements. Prefer a lazy, efficient ay to return instances of both into one array, as opposed to running a command twice and combining the arrays

Doug Fir
  • 14,921
  • 39
  • 121
  • 228

1 Answers1

2

You can Get the URL of the element with id, example id="myLink" in the document:

var my_link = document.links.namedItem("myLink").href;

Or you can use querySelectorAll() like @Pointy mentioned in comment :

var my_link = document.querySelectorAll("a[href='/somelink']");

Hope this helps.

Zakaria Acharki
  • 63,488
  • 15
  • 64
  • 88