3

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>
asked Sep 21, 2011 at 6:33

2 Answers 2

8

Try:

$(document).ready(function () {
 $('a').click(function () {
 var $newValue = 'newvalue';
 $(this).attr('href',$(this).attr('href').replace('somevalue-abc', $newValue));
 });
});
answered Sep 21, 2011 at 6:41

1 Comment

works perfect (and so simple I can shoot myself not finding this myself). Thx!!
5
$('a').click(function() {
 var newValue = 'newvalue'; // pls don't use a $ on variables that aren't jquery objects
 var src = $(this).attr('href');
 $(this).attr('href', src.replace(/somevalue\-abc/, newvalue));
});
answered Sep 21, 2011 at 6:39

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.