How do I select all links on a page that contain a certain keyword. I want all links that contain 'amazon' in the link. Some links might be https, or http so I can't do it like so
let amazonLinks = document.querySelectorAll('[href*=""]');
How do I select all links on a page that contain a certain keyword. I want all links that contain 'amazon' in the link. Some links might be https, or http so I can't do it like so
let amazonLinks = document.querySelectorAll('[href*="https://www.amazon"]');
you can pick all the links in the page using the below selector:
document.querySelectorAll('a[href*="amazon"]').forEach(function(a){
console.log(a.href)});
Cheers! hope this helps...
Here are the wildcards for the querySelector:
[attr^='someValue']
will match all ids starting with someId.
[attr$='someValue']
will match all ids ending with someId.
[attr*='someValue']
will match all ids containing someId.
If you need either http
or https
you should query as ://amazon.
Using jQuery :
$('a[href*="/YOUR-TEXT-HERE/"]').attr('href')