0

I am trying to add dates to a slider and need to increment the values on click. I currently start with the year - 2

$('#adddates').click(function() {
    var year = 2;
    $("#slider").dateRangeSlider({
        bounds: {
            min: new Date(Today.getFullYear() - ++year, 0, 1),
            max: new Date(Today.getFullYear(), Today.getMonth(), Today.getDate())
        }
    });
});

On click I am only getting an additional year. I need to increase the number of the year variable from 2 on each click. How can this be achieved?

3
  • You need some sort of global to maintain client state. Commented Jan 27, 2016 at 19:23
  • On click you are getting an additional year, meaning one. That is evident when using ++year. So can you clarify what you mean then by saying increase the number of the year variable from 2? Do you mean using year += 2 ? Commented Jan 27, 2016 at 19:25
  • 1
    He wants to maintain the state of year such that it doesn't get reset, at least that is what it sounds like. Commented Jan 27, 2016 at 19:26

1 Answer 1

1

If you're asking how to maintain the year value declare it outside of your click event:

$( document ).ready(function() {
    var year = 2; // abstracting the year away from the click function
    $('#adddates').click(function() {
        $("#slider").dateRangeSlider({
            bounds: {
                min: new Date(Today.getFullYear() - ++year, 0, 1),
                max: new Date(Today.getFullYear(), Today.getMonth(), Today.getDate())
            }
        });
    });
});

Please note however, if for any reason you do some sort of server side post back this value will be reset.

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

1 Comment

Wouldn't it make more sense to keep it inside the $(document).ready but outside the $("#adddates').click(function ?

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.