0

I have a body class called "bg-success". And also 4 button with different class value. Now if I click any button its working perfectly it'll assign the new class value. But if I reload my page mean that newly assigned class is changed to default class value "bg-success".

Now, What I want is that newly assigned value has to be active until I close the page, That value do not change for normal page reloading. Thanks :)

My Code

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
  </head>
<body class="bg-success">

<h1>Hello World</h1>

<button onclick="myFunction()">yellow</button>
<button onclick="myFunction1()">blue</button>
<button onclick="myFunction2()">grey</button>
<button onclick="myFunction4()">green</button>


<script>
function myFunction() {
  document.getElementsByTagName("body")[0].setAttribute("class", "bg-warning"); 
}
function myFunction1() {
  document.getElementsByTagName("body")[0].setAttribute("class", "bg-primary"); 
}
function myFunction2() {
  document.getElementsByTagName("body")[0].setAttribute("class", "bg-secondary"); 
}
function myFunction4() {
  document.getElementsByTagName("body")[0].setAttribute("class", "bg-success"); 
}
  
</script>
</body>
</html>
  • Codepen Link : https://codepen.io/JOHNakl/pen/dyXgOYd?editors=1000 – Janaki Raman Nov 09 '20 at 02:44
  • Does this answer your question? [Persist variables between page loads](https://stackoverflow.com/questions/29986657/persist-variables-between-page-loads) – jmargolisvt Nov 09 '20 at 02:47
  • Nope, do you have any other solution. – Janaki Raman Nov 09 '20 at 02:51
  • To be clear... you tried using query strings, web storage, and cookies and none of those are an acceptable solution? You could use a persistent state container like Redux as well, of course. What about the other solutions did you find unsatisfactory? – jmargolisvt Nov 09 '20 at 02:57

0 Answers0