Question

I'm making a next page system in which you click on 'next' and it takes you to the next set of elements.

This is the button in php:

    <input id="nextbtn" type="button" value="Next"></input>

This is the jquery function:

$('#nextbtn').click(function(){

});

The URL is: www.domain.com?var1=0&var2=20

I want to increment var1 and var2 by 20 everytime the 'next' button is clicked. Please tell me the code I should write in the jquery function so that this works.

Was it helpful?

Solution

You can get query string current value:

function getQueryStringParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Now call this function in your next button click and add increment value:

var var1 = parseInt(getQueryStringParameterByName('var1'),10)+20;
var var2 = parseInt(getQueryStringParameterByName('var2'),10)+20;

and set it to url and redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top