1

I have need to find and replace all link that match for example i am looking for

.domainname.com/Home and i want to replace it with .domainname.com/ i only want to remove part of link

$('a').each(function() {
  var $this = $(this),
    aHref = $this.attr('href'); //get the value of an attribute 'href'
  $this.attr('href', aHref.replace('domainname.com/Home', 'domainname.com/')); //set the value of an attribute 'href'
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="rd-navbar-nav">
  <li>
    <a href="https://domainname.com/Home">
      <span>Home</span>
    </a>


  </li>
  <li>
    <a href="https://domainname.com/about-us">
      <span>About Us</span>
    </a>
  </li>
  <li>
    <a href="https://domainname.com/contact-us">
      <span>Contact Us</span>
    </a>
  </li>


</ul>

2
  • 4
    What have you tried to so far? .replace? .attr("href")? Commented Aug 20, 2019 at 11:09
  • 3
    If you answered your own question feel free to post an answer here below instead of an edit to your question. Commented Aug 20, 2019 at 11:23

3 Answers 3

2

You can use URL api

function changeURL(){
  let links = document.getElementsByTagName('a')
  Object.values(links).forEach(link=>{
    let url = new URL(link.href)
    if(url.pathname === '/Home'){
      link.href = url.origin
    }
  })
}
<ul class="rd-navbar-nav">
  <li>
    <a href="https://domainname.com/Home">
      <span>Home</span>
    </a>
  </li>
  <li>
    <a href="https://domainname.com/about-us">
      <span>About Us</span>
    </a>
  </li>
  <li>
    <a href="https://domainname.com/contact-us">
      <span>Contact Us</span>
    </a>
</li>
</ul>

<button onclick="changeURL()">Change urls</button>

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

Comments

1

Combining

I would suggest:

$('[href*="domainname.com/Home"]').attr('href', 
             (i, attr) => attr.replace(/domainname.com\/Home/g, 'domainname.com/'));

console.log($('li:first').html());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<ul class="rd-navbar-nav">
    <li>
        <a href="https://domainname.com/Home">
            <span>Home</span>
        </a>
    </li>
    <li>
        <a href="https://domainname.com/about-us">
            <span>About Us</span>
        </a>
    </li>
    <li>
        <a href="https://domainname.com/contact-us">
            <span>Contact Us</span>
        </a>
    </li>
</ul>

Comments

-1

you selected also jquery tag.. then here: https://codepen.io/guymevents/pen/jONMeoL

$("a").each((i,item) => {
    var jItem = $(item)
    jItem.prop("href", 
        jItem.prop("href").replace(/domainname.com\/Home/,'domainname.com')
    )
})

5 Comments

Didn't downvote, but seems incomplete or was just copied from somewhere without updating for OPs requirement (to change the url)
I did downvote because this example doesn't fit OP's question
@LaurentS. ok, then i changed the example :)
So the OP asks for replacing ".domainname.com/Home" by ".domainname.com/", and you propose a solution replacing actually nothing by "guy". I don't see any good in that and your edition makes no further step towards a correct answer
@LaurentS. Thank you for your feedback. now i understand why you voted down.

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.