JSFiddle - React, Tailwind, and code Playground
by Rajeswaritest
HTML
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.4/jquery.mobile-1.4.4.min.js"></script>
<div style="width: 100%;">
<input type="text" tabindex="0" value="Flipswitch"/>
</div>
<div tabindex="0" id="chkdiv" onKeyPress="changeCheckbox(this)" onclick="changeCheckbox(this)" class="ui-flipswitch ui-shadow-inset ui-corner-all ui-flipswitch-active">
<a id="chkPref" class="ui-flipswitch-on ui-btn ui-shadow ui-btn-inherit" role="switch" aria-checked="false" aria-disabled="false" aria-label="No" onclick="javascript:changeCheckbox(this)" onKeyPress="changeCheckbox(this)" href="javascript:;">Yes</a>
<span class="ui-flipswitch-off">No</span>
<input data-role="flipswitch" type="checkbox" hidden="true" class="ui-flipswitch-input"/>
</div>
<div style="width: 100%;">
<input type="text" tabindex="0" value="Flipswitch"/>
</div>
CSS
.ui-flipswitch {
display: inline-block;
vertical-align: middle;
width: 5.875em;
height: 1.875em;
border-width: 1px;
border-style: solid;
margin: .5em 0;
overflow: hidden;
}
.ui-flipswitch.ui-flipswitch-active {
padding-left: 4em;
width: 1.875em;
}
.ui-flipswitch-input {
position: absolute;
height: 1px;
width: 1px;
margin: -1px;
overflow: hidden;
clip: rect(1px,1px,1px,1px);
border: 0;
outline: 0;
filter: Alpha(Opacity=0);
opacity: 0;
}
.ui-flipswitch .ui-btn.ui-flipswitch-on, .ui-flipswitch .ui-flipswitch-off {
float: left;
height: 1.25em;
margin: .0625em;
line-height: 1.65em;
}
.ui-flipswitch .ui-btn.ui-flipswitch-on {
width: 1.25em;
padding: 0;
text-indent: -2.6em;
text-align: left;
border-width: 1px;
border-style: solid;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
border-radius: inherit;
overflow: visible;
color: inherit;
text-shadow: inherit;
}
.ui-flipswitch .ui-flipswitch-off {
padding: 1px;
text-indent: 1em;
}
.ui-shadow-inset {
box-shadow: none;
}
.ui-corner-all {
-webkit-border-radius: .3125em;
border-radius: .3125em;
}
.ui-flipswitch-input {
position: absolute;
height: 1px;
width: 1px;
margin: -1px;
overflow: hidden;
clip: rect(1px, 1px, 1px, 1px);
border: 0;
outline: 0;
filter: alpha(opacity=0);
opacity: 0;
}
.ui-btn.ui-flipswitch-on {
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
box-sizing: content-box;
background-color: #000;
border-color: #000;
color: #000;
padding: 0;
-ms-border-radius: 5px;
border-radius: 5px;
}
JavaScript
function changeCheckbox(event) {
let item = document.getElementById('chkPref');
alert(item.getAttribute('aria-checked'))
let mainDiv= document.getElementById('chkdiv');
switch(item.getAttribute('aria-checked')) {
case "true":
item.setAttribute('aria-checked', "false");
item.setAttribute('aria-label', "No");
mainDiv.setAttribute('class', "ui-flipswitch ui-shadow-inset ui-bar-inherit ui-corner-all");
break;
case "false":
item.setAttribute('aria-checked', "true");
item.setAttribute('aria-label', "Yes");
mainDiv.setAttribute('class', "ui-flipswitch ui-shadow-inset ui-bar-inherit ui-corner-all ui-flipswitch-active");
break;
}
}