0

So I made an HTML button and on clicking to it, a CSS class is added to it using some Jquery/javascript logic. Is there any way I can store this animation on my database so that on page refresh the class should still stay. Or if I can do that with Javascript. A little guidance would be appreciated.

Regards Fanceh

1 Answer 1

1

You have a lot of ways to achieve what you want. I would recommend using localStorage of user browser (docs) to save that class:

localStorage.setItem('buttonClass', 'some-class');

And then later when you refresh the page just check that if localStorage has buttonClass key set.

const savedClass = localStorage.getItem('buttonClass');
if (savedClass) {
  // automatically add to your button
} else {
  // behave as usual
}

If you think that it is not permanent enough you can use Django session to store that value. This is better than storing it on some other table but much worse than using JavaScript along with local storage:

def your_view(request):
    button_class = request.session.get('saved_button_class')
    context = {
        'saved_button_class': button_class
    }
    render(request, 'templates/index.html', context=context)

In your template check for saved_button_class variable.

And you must implement new view that will accept AJAX request, telling that user have clicked that button.

from django.http import JsonResponse

def ajax_view(request):
    request.session['saved_button_class'] = 'button_class'
    return JsonResponse({'saved': True})

Then on page refresh, you will have saved_button_class on user's session already

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

1 Comment

That's what I've been looking for! Thanks alot sir :)

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.