html5 - how to keep a button disabled in html even after refreshing the browser -


i used following code create 2 buttons connected. 1 enabled when other disabled , vice versa. problem when refresh browser page buttons revert original state, want them stay in current state when refresh page , should change state on button press manually.how can achieve this.

thanks in advance.

<button onclick="document.getelementbyid('start').disabled=true;document.getelementbyid('stop').disabled=false;" type="submit" class="positive" name="start" id="start">start</button>  <button onclick="this.disabled=true;document.getelementbyid('start').disabled=false;" type="submit" class="negative" name="stop" id="stop">stop</button>     

i have idea using session cookies can have no knowledge of how access them.

simply use disable attribute of button

<button onclick="this.disabled=true;document.getelementbyid('start').disabled=false;" type="submit" class="negative" name="stop" id="stop" disabled>stop</button>    

edit:

i gave idea how it. i.e. set disabled property of button. upto when wish disable i.e. on server or on client , based on condition.

in case want set on server, you'll have send selected button server via ajax call , set session on server (you haven't mentioned server side scripting language otherwise have shown code set session)

in case want set on client side, can use cookies or localstorage(based on target browser) , use previuosly selected button on screen load.

edit2

to use session in php, use following code

function changebuttonstate(id) {     document.getelementbyid('start').disabled = false;     document.getelementbyid('stop').disabled = false;     document.getelementbyid(id).disabled = true;     if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari         xmlhttp = new xmlhttprequest();     } else { // code ie6, ie5         xmlhttp = new activexobject("microsoft.xmlhttp");     }     xmlhttp.onreadystatechange = function () {         if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {             //success         }     }     xmlhttp.open("get", "savestate.php?button=" + id, true);     xmlhttp.send(); } 

html

    <?php     $id=$ _session[ 'buttonid'];     if(strcmp ( $id , "start" ))     {      <button onclick="changebuttonstate('start')" type="submit" class="positive" name="start" id="start">start</button>     <button onclick="changebuttonstate('stop')" type="submit" class="negative" name="stop" id="stop" disabled>stop</button>    }    else    {     <button onclick="changebuttonstate('start')" type="submit" class="positive" name="start" id="start" disabled>start</button>     <button onclick="changebuttonstate('stop')" type="submit" class="negative" name="stop" id="stop">stop</button>    }  ?> 

php code savestate.php

<?php     $id=$_get[ "button"];     $_session[ 'buttonid']=$ id;  ?> 

hope helps.


Comments