I just finished creating a fancy checkbox and am hoping for some tips to help improve my code, as I am still learning.
$(document).ready(function(){
function checkbox(){
var y = $("#yon");
var n = $("#non");
var cx = $("#chex")
var cbx = $("#checkbox");
var chval = false;
$(".cspan").click(function(){
$(n).toggleClass("no");
$(y).toggleClass("yes");
if(chval){
$(cx).text("\u2716");
$(cbx).prop('checked', false);
chval = false;
}
else{
$(cx).text("\u2714");
$(cbx).prop('checked', true);
chval = true;
}
});
}
checkbox();
});
#checkcon p{
}
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label span {
display:inline-block;
width: 160px;
height: 60px;
border-radius: 25px;
margin:-1px 4px 0 0;
vertical-align:middle;
background-color: #ff3a31;
cursor:pointer;
transition: background-color 100ms linear;
position:absolute;
}
input[type="checkbox"]:checked + label span {
background-color: #00cc66;
transition: background-color 100ms linear;
}
input[type="checkbox"] {
}
.checkemb{
position:relative;
display:inline-block;
margin-right:10px;
margin-left:10px;
font-family:"Arial Black", Gadget, sans-serif;
color:#ffffff;
}
#chex{
margin-right:25px;
margin-left:25px;
}
p.yes{
color:#990800;
transition: color 100ms linear;
}
p.no{
color:#008040;
transition: color 100ms linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<div id="checkcon">
<input id="checkbx" type="checkbox"/>
<label for="checkbx"><span class="cspan"></span></label>
<p class="checkemb yes" id="yon">Yes</p><p class="checkemb x" id="chex"><strong>✖ </strong></p><p class="checkemb" id="non">No</p>
</div>
</body>