-2

I need to change the value of query parameter present in the given URL using jQuery.

There are 2 possible URL's like,

www.domain.com/?id=10&name=xxx

and

www.domain.com/?name=xxx&id=10

I need to change the value of parameter id in this URL into something like www.domain.com/?id=15&name=xxx. Regex seems to be the solution for this, but it looks confusing to me.

Possible solution:

Select the string between "id=" and "&" (or) string from "id=" to the end of string and replace it with desired value 15.

Does anyone have solution for this or any other better solution?

Thanks in advance!

6
  • If you google search your title you will get a lot of answer to your question Commented Dec 13, 2018 at 6:42
  • Of course I did. But none seems like the exact solution. Commented Dec 13, 2018 at 6:44
  • Have you had a look at URLSearchParams? There's even a JS polyfill for browsers that don't support it natively. Commented Dec 13, 2018 at 6:49
  • Your question is unclear. Where is this URL that you are wanting to change? Is it the actual URL for the page on which you are running? Is it an href in an <a>? In addition, you also need to tell us exactly what you want, not "something like ..." Does what you said mean that you want both www.domain.com/?id=10&name=xxx and www.domain.com/?name=xxx&id=10 changed to www.domain.com/?id=15&name=xxx? Do you really care what the order is (it's possible that order matters, but usually it doesn't. You just haven't told us what you actually want)? Commented Dec 13, 2018 at 6:58
  • 1
    Editing Questions to improve them (e.g. clarification, adding additional information, etc.) is encouraged. However, editing a Question to change it into a different question which results in invalidating an Answer, is against policy on Stack Overflow. Your edit here did so. The policy is that other users should proactively revert such changes. I have done so here. You are encouraged to ask a new Question, perhaps with a link to this one for additional context. We want to help, but your new/additional issue needs to be a new Question. Commented Dec 13, 2018 at 7:27

2 Answers 2

1

var url="www.domain.com/?id=10&name=xxx";
changeUrl("id",15);
changeUrl("name","sumesh");
function changeUrl(key,value){
  var patt = new RegExp(key+"=[a-zA-Z0-9]+");
  var matches = patt.exec(url);
  var id2 = matches[0];
  url = url.replace(id2, key+'='+value);
  console.log(url);
}

Use RegExp to achieve this.

Sign up to request clarification or add additional context in comments.

8 Comments

The parameter "id" is just an example. I need generic regular expression which also must suite for changing the parameter "name".
Generic means how ? do want change entire query string?
You have given regex for integer. Consider I have a function which accepts parameter and the value. If I pass (id, 15) or (name, yyy) this method should work.
At least use [0-9]+, or better, just the character class \d+.
@VinothKumar That's not what your question asks for. If you wanted that, then you needed to have it in your question and before people answered. You will need to create a new question to ask for the generalization that you appear to want, as changing a question such that you invalidate answers is against policy.
|
0

Why don't you use simple replace?

var newUrl = location.href.replace("id="+10, "id="+15);

I found this solution from an answer to the question Change url query string value using jQuery (marked as duplicate).

1 Comment

But I don't know the value "10" since it is random. How can I handle this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.