0

I have an html code to download files like this:

<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5.exe" rel="nofollow noopener noreferrer">Download (Installer)</a>
<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5p.exe" rel="nofollow noopener noreferrer">Download (Portable)</a>

I want all the URLs in the class="wp-block-button__link tag to change like this:

<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="mydomain.com/get/?url=https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5p.exe" rel="nofollow noopener noreferrer">Download (Portable)</a>
<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="mydomain.com/get/?url=https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5.exe" rel="nofollow noopener noreferrer">Download (Installer)</a>

I am currently changing it using Javascript like this:

let a = document.querySelector('.wp-block-button__link');
a.href = "mydomain.com/get/?url=" + a.href
console.log(a.outerHTML)

The javascript code that I use now can only change one URL, I want to change all the URLs in the class="wp-block-button__link" tag.

How do I make the javascript code I use change all URLs in the tag class="wp-block-button__link", not just one URL?

6
  • 2
    use document.querySelectorAll and loop through it Commented Apr 12, 2019 at 10:15
  • @AswinKumar can you write the code here ? Commented Apr 12, 2019 at 10:17
  • document.querySelectorAll('.wp-block-button__link').forEach(a => { a.href = "mydomain.com/get/?url=" + a.href }); Commented Apr 12, 2019 at 10:19
  • thank you @briosheje , this code works well. Commented Apr 12, 2019 at 10:28
  • @RenomuReza accept any of the below answers, they show the same code. Commented Apr 12, 2019 at 10:29

2 Answers 2

2

Use querySelectorAll and forEach:

let allLinks = document.querySelectorAll('.wp-block-button__link');
allLinks.forEach(a => {
    a.href = "mydomain.com/get/?url=" + a.href;
    console.log(a.outerHTML);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Using document.querySelectorAll() and loop through it with forEach()

let a = document.querySelectorAll('.wp-block-button__link');
a.forEach((i) => i.href = "mydomain.com/get/?url=" + i.href);
<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5.exe" rel="nofollow noopener noreferrer">Download (Installer)</a>
<a class="wp-block-button__link has-background has-vivid-green-cyan-background-color" href="https://github.com/pbatard/rufus/releases/download/v3.5/rufus-3.5p.exe" rel="nofollow noopener noreferrer">Download (Portable)</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.