How to clear the parameter value to enter new values?

2
&fespecial=lanccontrucao&fespecial=novopronto

Here is a parameter that persists when executing the code below:

// $("#fe1").click(function(){
// $("#fe2").click(function(){

$("#fe3").click(function(){

    var url         = window.location.href;
    var parametro   = "fespecial";
    var valor       = $(this).val();
    var master      = url+"&"+parametro+"="+valor;

    if( $("#fe2").is(":checked") ){
        ///////////////////////////////////////
        // alert("Checked " +master);
        window.location.href = master;

    } else {
        ///////////////////////////
        // alert("Unchecked " +master); 

    }

});

I need to clear the current values of the fespecial parameter to insert a new value into this same parameter that is part of a url. Each time I click on one of these ids it inserts another parameter fespecial with a new value and I would like to clear what exists to insert the new value of this parameter.

    
asked by anonymous 23.02.2015 / 15:57

1 answer

1

Use the $.param method of jQuery to make work easier. Also, reformulate the way you get the url.

See:

var parametro = $.param({
   fespecial: valor
});
var master = location.protocol + '//' + location.host + location.pathname + '?' + parametro

So you will mount your url according to the need:)

Another way to get the current url without the parameters can perhaps be done with the split function. In this case, you can use your code by only changing this snippet:

var url = location.href.split('?')[0] // pega o primeiro item gerado antes do "?"
    
23.02.2015 / 16:17