daniels_pa daniels_pa - 3 years ago 219
HTML Question

programmatically change flipswitch value

I have tried fiddling with some HTML/JS/CSS
But got stuck on how to change the flipswich dynamically to a desired state.

CSS from here https://proto.io/freebies/onoff/

.onoffswitch {
position: relative; width: 90px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #999999; border-radius: 20px;
}
.onoffswitch-inner {
display: block; width: 200%; margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 10px;
background-color: #34A7C1; color: #FFFFFF;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 10px;
background-color: #EEEEEE; color: #999999;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 18px; margin: 6px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 56px;
border: 2px solid #999999; border-radius: 20px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}


HTML/js

<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<span id="innerswitchid" class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
<script type="text/javascript">
function on()
{
document.getElementById("innerswitchid").className=document.getElementById("innerswitchid").className+ "onoffswitch-inner:before";
}
function off()
{
document.getElementById("innerswitchid").className=document.getElementById("innerswitchid").className+ "onoffswitch-inner:after";
}
</script>
<button onclick="on()">ON</button>
<button onclick="off()">OFF</button>


However that just crashes the flipswich. How can I change the flipswich dynamically to a desired state using js to change the css of the component?

Answer Source

Updating your code to the following:

HTML

<button id="js-btn--on">
  ON
</button>
<button id="js-btn--off">
  OFF
</button>

JS

   document.getElementById("js-btn--on").onclick = on;
   document.getElementById("js-btn--off").onclick = off;

   function on() {
     document.getElementById('myonoffswitch').checked = true;

   }

   function off() {
     document.getElementById('myonoffswitch').checked = false;
   }

You can toggle the checkbox and let the CSS do what it intends to accomplish rather than mixing JS and CSS.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download