-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

Nita
  • 561
  • 5
  • 28
  • Given that the input appears to be a free text field, how can you be sure that `yy` will *always* be the last parameter? – Rory McCrossan Mar 29 '18 at 09:12
  • 1
    Perhaps you could use this and then just replace the yy parameter: https://stackoverflow.com/questions/4297765/make-a-javascript-array-from-url – Pete Mar 29 '18 at 09:13
  • yy is always the last parameter – Nita Mar 29 '18 at 09:19

2 Answers2

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>
eborrallo
  • 750
  • 1
  • 8
  • 17
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

Nita
  • 561
  • 5
  • 28