Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update querystring value in href with jQuery

I have 3 href's that have a different structure and all of them have the same hard coded parameter (in this example value=somevalue-abc).

How can I update the link and overwrite the value with a new value?

jQuery

$(document).ready(function () {

    $('a').click(function () {

        var $newValue = 'newvalue';

        //here a function that overwrites the parameter 'somevalue-abc' with $newValue

    });

});

HTML

<a class="demo" href="http://www.mydomain.com/page.aspx?demo&value=somevalue-abc">link 1</a>
<a class="real" href="http://www.mydomain.com/?value=somevalue-abc#go=yes">link 2</a>
<a class="outgoing" href="http://www.mydomain.com/page.aspx?value=somevalue-abc">link 3</a>
like image 887
NicoF Avatar asked Dec 29 '25 06:12

NicoF


1 Answers

Try:

$(document).ready(function () {
    $('a').click(function () {
        var $newValue = 'newvalue';
        $(this).attr('href',$(this).attr('href').replace('somevalue-abc', $newValue));
    });
});
like image 59
ysrb Avatar answered Dec 30 '25 21:12

ysrb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!