1

I have many links from the same web but with different dirreciones

<a href="somelink.com/1" title="this link">
</a>
<a href="somelink.com/2" title="this link">
</a>
<a href="somelink.com/3" title="this link">
</a>
<a href="somelink.com/3" title="this link">
</a>

He tried to use the code but it does not work for me since he searches for a specific url and not all

var a = document.querySelector('a[href="somelink.com"]');
if (a) {
  a.setAttribute('href', 'replacedlink.com')
}
<a href="somelink.com" title="this link">
</a>

How could I do it in a massive way and do it to all the url of a web site in estecifico for example: somelink.com

3 Answers 3

1

You could use an attribute selector ^= to check if the href starts with somelink.com. Then you could replace the url :

document.querySelectorAll('a[href^="somelink.com"]').forEach(
  x => x.href = x.href.replace("somelink.com", "replacedlink.com")
);
<a href="somelink.com/1" title="this link">a
</a>
<a href="somelink.com/2" title="this link">b
</a>
<a href="somelink.com/3" title="this link">c
</a>
<a href="somelink.com/3" title="this link">d
</a>
<a href="otherlink.com/3" title="this link">e
</a>

If you want to replace the whole link, you could set the href attribute to the new link:

document.querySelectorAll('a[href^="somelink.com"]').forEach(
  x => x.href = "replacedlink.com"
)
<a href="somelink.com/1" title="this link">a
</a>
<a href="somelink.com/2" title="this link">b
</a>
<a href="somelink.com/3" title="this link">c
</a>
<a href="somelink.com/3" title="this link">d
</a>
<a href="otherlink.com/3" title="this link">e
</a>

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but how would you replace the whole url osea somelink.com/1 -> replacedlink.com, removing the parameter / 1
@juan In that case you can set the href attribute to the new link. I have updated my answer with an example.
0
 Try adding class to the links and then use get element by class and replace the link
       <a href="www.google.com" class="abc">my url</a>
        var i=document.getElementByClass("abc");
          for(let j=0;j<i.length;j++){
           i[j].href='newUrl';
            }

2 Comments

in my case the url have no class, they are stored in a database
then u can concat the url on click like if u have base url www.facebook.com and you want to go to setting page y can add the value on click so the url becomes www.facebook.com/settings
0

For multiple items we can replace this way:

var a = document.querySelectorAll('.your_attr_class').forEach( (item)=>{ 

      if(item.href.startsWith('http://somelink.com')){
        item.href.replace('http://somelink.com','http://replacewith.com');
      }

});

2 Comments

I do not work, my friend, because the urls have parameters
You can check the href with startsWith method and then can replace as you want.

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.