Change Href with jQuery

1

I'm trying to copy the href from the buy-button class to .test, however changing the "redirect = true" to false.

I can only 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>
    
asked by anonymous 12.05.2014 / 22:42

2 answers

5

Have you tried a simple replace ? Since you already have 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;
    
12.05.2014 / 22:49
1

See if this is 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;
  }
}

How to use:

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

Credits: SO-En

    
12.05.2014 / 22:46