Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - get all anchor tags and compare them to an array

Tags:

javascript

I have been trying forever but it is just not working, how can I check the array of urls I got (document.getElementsByTagName('a').href;) to see if any of the websites are in another array?

like image 967
Dr Hydralisk Avatar asked Dec 03 '22 04:12

Dr Hydralisk


1 Answers

getElementByTagName gives you a nodelist (an array of nodes).

var a = document.getElementsByTagName('a');

for (var idx= 0; idx < a.length; ++idx){
    console.log(a[idx].href);
}

I really suggest that you use a frame work for this, like jquery. It makes your life so much easier.

Example with jquery:

$("a").each(function(){
  console.log(this.href);
});
like image 106
some Avatar answered Feb 23 '23 10:02

some