-3

I would like to replace part of the input value, to be exact posted value in the link. Value of yy parameter has to be replaced with (yyNewval).

   yyNewval = '3xxxx';
   returnlink = $("input[name=return]"); 
   returnlink.val;

will give you output of:

http://www.test.com/xx=1&yy=2xxxx

i would like to change the highlighted (2xxxx) so everything after (yy=) . I know how to replace entire input value, but will be more handy to alter only last parameter.

THX for your help in advance

3
  • Given that the input appears to be a free text field, how can you be sure that yy will always be the last parameter? Commented Mar 29, 2018 at 9:12
  • 1
    Perhaps you could use this and then just replace the yy parameter: stackoverflow.com/questions/4297765/… Commented Mar 29, 2018 at 9:13
  • yy is always the last parameter Commented Mar 29, 2018 at 9:19

2 Answers 2

3

try this , i think this is what you need :)

function getUrlYYY(){
  var url_string = "http://www.example.com/t.html?yyy=123456789"; //window.location.href
  var url = new URL(url_string);
  var yyy = url.searchParams.get("yyy");
  console.log('the parameter in url yyy is '+ yyy);
  returnlink = $("input[name=return]"); 
  returnlink.val(yyy);
}

function setUrlYYY(){
// Get the queryString module from:
// https://github.com/sindresorhus/query-string
  returnlink = $("input[name=return]"); 
  console.log(window.location.search);
  // ?yyy=123456789
  var parsed = queryString.parse(location.search);
  console.log(parsed);
  // {yyy: '123456789'}
  parsed.yyy =  returnlink.val();
  location.search = queryString.stringify(parsed);
  console.log(location.search);
}

function updateQueryStringParam(param, value) {
  baseUrl = [location.protocol, '//', location.host, location.pathname].join('');
  urlQueryString = document.location.search;
  var newParam = key + '=' + value,
  params = '?' + newParam;

  // If the "search" string exists, then build params from it
  if (urlQueryString) {
    keyRegex = new RegExp('([\?&])' + key + '[^&]*');
    // If param exists already, update it
    if (urlQueryString.match(keyRegex) !== null) {
      params = urlQueryString.replace(keyRegex, "$1" + newParam);
    } else { // Otherwise, add it to end of query string
      params = urlQueryString + '&' + newParam;
    }
  }
  window.history.replaceState({}, "", baseUrl + params);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name='return'>
<button onclick=getUrlYYY()>get YYY</button>
<br>
<button onclick=setUrlYYY()>set YYY 1</button>
or
<br>
<button onclick=updateQueryStringParam(yyy,$("input[name=return]").val())>set YYY 2</button>

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

6 Comments

yes, this is ok, but i need to replace value of yy in this input
you need change url parameter whit input , or change input whit url parameters ?? @Nita
i need to change value if (yy) parametr within input
maybe i'm not explainig my self properly. i updated question. i would like to replace value of yy parameter with the value of variable yyNewval. Hope is more clear now
both give me errors, but im working on something url functions now
|
0

Decided to go with regex.

  paymentcat = elem.attr('data-paymentcat');
  returnlink = $("input[name=return]");
  returnlinkVal = returnlink.val();
  reExp = /paymentcat=[a-zA-Z0-9 -'%]+/;
  newReturnlinkVal = returnlinkVal.replace(reExp, "paymentcat=" + paymentcat);
  returnlink.val(newReturnlinkVal);

Although would like to achieve similar with URL / URLSearchParams

Comments

Your Answer

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.