1

my url looks something like this

/myurl?code1=abcde&code2=fghijk&code3=lmnop&code4=qrstu&code5=vwxyz (up to max of 5 code variables)

I have an onclick event where I get the code variable, eg in example above it returns 'fghijk', but I don't know the param, eg code2. So I want to do two things:

1) find and remove the param & value from my url (if my onclick variable returns 'fghijk', in the above example my url becomes, /myurl?code1=abcde&code3=lmnop&code4=qrstu&code5=vwxyz)

2) after this I want to reset the param numbers so that my code params are sequential beginning from 1, so after number 1 above executes my url should become /myurl?code1=abcde&code2=lmnop&code3=qrstu&code4=vwxyz

$('.myelement').on('click', function() {
  var url = $('.myelement a').attr('href');
  var codevar = $('.myelement span').text();
  if(url +'contains('+codevar+')') {
   // strip the param and variable from the url here

   // now reset the url so code params are in number sequence
  }

});
1
  • Can I get an update, did you resolve your issue or are you still having errors? Commented Dec 11, 2013 at 23:20

3 Answers 3

1

you could do something like this:

$('.myelement').on('click', function() {
  var url = $('.myelement a').attr('href');
  var codevar = $('.myelement span').text();
  if(url.match(codevar)) {
   var queryString = url.substring(url.indexOf("?") + 1);
   var params = queryString.split("&");
   var codeIndex = 1;
   var newQuery = "";
   for (var i = 0; i < params.length; i++) {
     if (!params[i].match(codevar)) {
       newQuery += params[i].replace(/code[0-9]/, "code" + codeIndex);
       codeIndex++;
       if (i < params.length - 1) {
         newQuery += "&";
       }
     } 
   }
   url = url.replace(queryString, newQuery).replace(/&$/, ""); //new query string with the sequential parameters
  }

});

Hope this helps.

Regards,

Marcelo

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

1 Comment

this works well, in my example I'm left with a trailing "&" and if I remove the last variable from the url, I'm left with a ? on the end of my url, how can I remove this if its the last one?
1
  var codevar = 'fghijk';
  var url = $('.myelement a').attr('href');
  var param = '&'+url.substring(url.indexOf('?')+1);
  url = url.substring(0,url.indexOf('?')+1);
  if(param.indexOf(codevar) > -1) {
      var arr = param.split("&code");
      param = '';
      for(var i = 1;i<arr.length;i++){
          if(codevar == arr[i].substring(2))
              arr.splice(i, 1);
          param += 'code'+i+'='+arr[i].substring(2);
          if(i != arr.length-1)
              param +='&';
      }
      $('.myelement a').attr('href',url+param);

Example:

http://jsfiddle.net/trevordowdle/b9Hmb/1/

2 Comments

almost there, it works until I get to the last variable, then I get arr[i] is undefined, any ideas?
@uknowit2 Does this error show up in my fiddle? Can you create a fiddle with your code? You could try doing ;i<arr.length-1 instead of ;i<arr.length in the for loop.
0

You could split the url with the param as key.

var el = $('.myelement'),
    out = $("#output");

el.on('click', function (ev) {
    ev.preventDefault();
    var href = el.find("a").prop("href"),
        param = el.find("span").text(),
        idx = href.indexOf(param),
        arr = [], url;

    if (idx > -1) {
        arr = href.split(param);  // split href[0] param href[1]
        url = arr.join("mynewvalue");
        out.text(url);
    }
});

DEMO : http://jsfiddle.net/tive/EAsw3/

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.