Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.30 KB | None | 0 0
  1. <html>
  2. <head>
  3. <link rel="stylesheet" type="text/css" href="relay.css">
  4. <script type="text/javascript">
  5. function onoff(){
  6.  
  7.   currentvalue = document.getElementById('myonoffswitch').value;
  8.   console.log('Value is ' + currentvalue);
  9.   if(currentvalue == "Off"){
  10.     document.getElementById("myonoffswitch").value="On";
  11.   }else{
  12.      document.getElementById("myonoffswitch").value="Off";
  13.   }
  14.  
  15.   ajaxJSONPost("/api/relay",{pin: 1,state : document.getElementById("myonoffswitch").value}, function (resp){console.log('Response is: ',resp)})
  16. }
  17.  
  18. function ajaxJSONPost(url, jsondata, callback){
  19.     var xhr = new XMLHttpRequest();
  20.     xhr.open("POST", url);
  21.     xhr.setRequestHeader('Content-Type', "application/json;charset=UTF-8");
  22.     xhr.onreadystatechange = function () {
  23.         if (xhr.readyState == 4 && xhr.status == 200) {
  24.            callback(xhr.responseText);
  25.         }
  26.     }
  27.     xhr.send(JSON.stringify(jsondata));
  28. }
  29.  
  30. </script>
  31. </head>
  32. <body>
  33. <div class="onoffswitch">
  34.     <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" onclick="onoff();" value="Off">
  35.     <label class="onoffswitch-label" for="myonoffswitch">
  36.         <span class="onoffswitch-inner"></span>
  37.         <span class="onoffswitch-switch"></span>
  38.     </label>
  39. </div>
  40. </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement