Home > Blockchain >  Replace multiple urls with multiple classes
Replace multiple urls with multiple classes

Time:01-08

Looking to turn these two scripts into one.

var removeLocaleLink = $(".int-tooltip a, .int-tooltip");

      removeLocaleLink.each(function(){
        $(this).attr("href", (_, href) =>
      href.replace(/\/zh-hant\/|\/de\/|\/fr\/|\/es\/|\/pt-br\/|\/ja\/|\/ko\//, "/") ); 
    })
   var removeLocaleLinkOne= $(".int-tooltip");
      removeLocaleLinkOne.each(function(){
        $(this).attr("href", (_, href) =>
      href.replace(/\/zh-hant\/|\/de\/|\/fr\/|\/es\/|\/pt-br\/|\/ja\/|\/ko\//, "/") ); 
    })

I tried turning the variable to var removeLocaleLink = $(".int-tooltip, .int-tooltip a"); but I get an error and it only does the first item on the page.

CodePudding user response:

You can merge the two scripts into one by using this script:

var removeLocaleLink = $(".int-tooltip, .int-tooltip a");

removeLocaleLink.each(function(){
  $(this).attr("href", (_, href) => href.replace(/\/zh-hant\/|\/de\/|\/fr\/|\/es\/|\/pt-br\/|\/ja\/|\/ko\//, "/") ); 
});

This will select both elements with the class int-tooltip and a elements within elements with the class int-tooltip, and replace the URLs in their href attributes.

CodePudding user response:

Like this?

var removeLocaleLink = $(".int-tooltip a, a.int-tooltip");
removeLocaleLink.each(function() {
  let href = $(this).attr("href")
  href = href.replace(/\/zh-hant\/|\/de\/|\/fr\/|\/es\/|\/pt-br\/|\/ja\/|\/ko\//, "/");
  $(this).attr("href",href);
  console.log(this.href)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a  href="/zh-hant/">/zh-hant</a>
<span ><a href="/zh-hant/">/zh-hant</a></span>

  • Related