Reputation: 21232
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
Upvotes: 1
Views: 414
Reputation: 67505
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.
Upvotes: 2