How to find all the element which has a href attribute in it

How can i select all the element which has a href attribute in it and also want to change the href value by javascript

1 Like

If you’re not entirely sure how Document.querySelectorAll() works I can give you a brief overview.

const aTags = document.querySelectorAll('a') 
aTags.forEach(aTag => {
  aTag.href = "NewHrefAttribute"
})

You use document.querySelectorAll("a") to grab all of the a tags on the document. Then, using Array.forEach() we change each tags href value.

1 Like

First i want to select all the element of any website and then i want filter them by href attr and then finally i want to change them.

If I’m not mistaken, the only tags that should have the href attribute should be a tags. You can just look for all of the a tags and then use something like the .filter method to remove all of the a tags that don’t have a value in the href.

Here is a link to the filter method that I mentioned.

1 Like