TheDizzle
TheDizzle

Reputation: 1574

Persist class change on refresh in asp.net mvc or jquery

I have a script that changes certain classes on buttons when the button is clicked. When you click the type-button it selects it then when you click the assigned-button it refreshes the page to load the form for the combination you selected.

$(document).ready(function () {
    $(".type-button").click(function () {
        $(this).toggleClass("btn-thinkware btn-success");
        $(".type-button").not(this)
           .removeClass("btn-success")
           .addClass("btn-thinkware");
    });
    $(".assigned-button").click(function () {
        $(this).toggleClass("btn-thinkware btn-success");
        $(".assigned-button").not(this)
           .removeClass("btn-success")
           .addClass("btn-thinkware");
    });
});

is it possible to hold the selected buttons on refresh? It only has to hold it on refresh, doesn't have to persist indefinitely, like if they leave the page and come back.

So when the new form loads based on the combination, can I hold the selected buttons new classes after the page reloads?

Is there a better way to do this? or am I on the right track?

Upvotes: 1

Views: 283

Answers (2)

Dave Thieben
Dave Thieben

Reputation: 5427

since HTTP is stateless, you will need to persist your state somewhere. you could use AJAX to POST to a controller that stores the button state in Session, you could set a cookie value, or even set a querystring value.

A better solution may be to just refresh the part of the page that needs to be refreshed and leave your buttons.

Upvotes: 1

user4843530
user4843530

Reputation:

It seems like you are trying to implement a radio button here using two different buttons and changing their classes depending on which one the user clicks. If that is really the case, I would go ahead and use an asp:RadioButton, which is stateful and will persist across page refreshes.

Upvotes: 0

Related Questions