Change Href with jQuery

Asked

Viewed 329 times

1

I’m trying to copy href from the buy-button class to . test, but change "redirect=true" to false.

I can just copy, but not change the redirect. Can anyone help me?

<a class="buy-button" href="/checkout/cart/add?sku=17839&amp;qty=1&amp;seller=1&amp;redirect=true&amp;sc=1" style="display:block">EU QUERO</a>
<a class="teste" href=""><p> Botao </p></a>

2 answers

5


You tried a simple replace? Whereas you have already obtained the URL:

var url = "/checkout/cart/add?sku=17839&amp;qty=1&amp;seller=1&amp;redirect=true&amp;sc=1";
url = url.replace("redirect=true", "redirect=false");
seulink.href = url;
  • so it gets simpler, but loses the air of javascript kkk magic (Brinks)

  • @bfavaretto would be exactly that, I ended up forgetting the replace, it worked right.

  • kkkkkkk yes Tafarel

  • Ô @Tafarel my answer just accepted and I missed the chance to say "go that is yours!" :)

1

See if that’s what you want.

function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}

Mode of use:

var newUri = updateQueryStringParameter("minhaurl.com?updateIsto=true","updateIsto",false);

Credits: SO - En

Browser other questions tagged

You are not signed in. Login or sign up in order to post.