Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  149] [ 7]  / answers: 1 / hits: 28580  / 7 Years ago, fri, march 17, 2017, 12:00:00

I have lots of pages with lots of links. I want to replace some specific links with another link.



what I wish to do here is find the href attribute of <a> and replace it with desired link



Here is the HTML code



<div class=one>
<div class=item>
<a href=somelink.com>click</a>
</div>
</div>


and I want to change HTML to



<div class=one>
<div class=item>
<a href=replacedlink.com>click</a>
</div>
</div>

More From » jquery

 Answers
48

One way could be is to use the href value to find the anchor





var a = document.querySelector('a[href=somelink.com]');
if (a) {
a.setAttribute('href', 'replacedlink.com')
}

<a href=somelink.com title=this link>
</a>




[#58509] Wednesday, March 15, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
analiseb

Total Points: 252
Total Questions: 96
Total Answers: 106

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
analiseb questions
;