JSFiddle - React, Tailwind, and code Playground

by chauhangs

HTML

<div class="switch-wrapper"> <span class="slider-button">NO</span>
</div>
<input type="text" class="slider-value" value="false">

CSS

.switch-wrapper {
    position: relative;
    display: block;
    margin: 0;
    padding: 0;
    margin: 0 auto;
    width: 50px;
    height: 20px;
    background-color: #404040;
    -moz-border-radius: 5px;
    border-radius: 5px;
}
.switch-wrapper .slider-button {
    display: block;
    margin: 0;
    padding: 1px;
    width: 25px;
    height: 15px;
    line-height: 15px;
    background: #d37a16;
    -moz-border-radius: 5px;
    border-radius: 5px;
    border: 1px solid #70430e;
    -webkit-transition: all 0.25s ease-in-out;
    -moz-transition: all 0.25s ease-in-out;
    transition: all 0.25s ease-in-out;
    color: #fff;
    font-family:Helvetica;
    font-size:11px;
    font-weight: bold;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    text-align: center;
    cursor: pointer;
}
.switch-wrapper .slider-button.on {
    margin-left: 20px;
    background: #2372c6;
    border: 1px solid #0f3f74;
}
.switch-wrapper .slider-button:before {
    position: absolute;
    display: block;
    margin: 0;
    padding: 0;
    width: 25px;
    height: 5px;
    background: rgba(255, 255, 255, 0.25);
    content:'';
    -moz-border-radius: 15px;
    border-radius: 15px;
}

JavaScript

$('.slider-button').toggle(function () {
    $(this).addClass('on').html('YES');
    $(this).next(".slider-value").val(true);
}, function () {
    $(this).removeClass('on').html('NO');
    $(this).next(".slider-value").val(false);
});