0

I am changing image on click, the code for that is below, but when I reload the browser it resets the src to default. I would like to retain the image src even after page reload. How can I do that?

<img src="unmark.png" onclick="changeImage()" id="alert_img"> 
function changeImage() {
    var image = document.getElementById('alert_img');
    if (image.src.match("unmark")){
        image.src = "mark.png";                            
    }  else {
        image.src = "unmark.png";        
    }                
};
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Pramod
  • 1
  • You can use `localStorage`, `sessionStorage` or cookies to achieve this, depending on the length of time you want the data to be kept. – Rory McCrossan May 18 '16 at 10:33
  • localstorage, cookies you can try either of them. – Manish May 18 '16 at 10:34
  • Javascript is client-side and will only run when the page is ready, once you reload/refresh the page all changes via javascript will be reset back to the normal/original state. If you want this to change for individual people then cookie/local storage would be a good way to get what you want but if you want the changes to apply for all clients then you will want to look at using storage on your host, database maybe... – NewToJS May 18 '16 at 10:37

0 Answers0