Skip to content

Instantly share code, notes, and snippets.

@arvindang
Last active December 29, 2015 04:59
Show Gist options
  • Save arvindang/7619073 to your computer and use it in GitHub Desktop.
Save arvindang/7619073 to your computer and use it in GitHub Desktop.
Three way toggle
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<!-- <a href="#">
<img class="static" src="http://placekitten.com/100/100" alt="">
</a>
-->
<input type="checkbox" id="checkbox">
<label for="checkbox"><img src="http://placekitten.com/100/100" alt=""></label>
</body>
</html>
img {
border-radius: 50%;
border: 4px solid transparent;
}
input[type=checkbox] {
display: none;
}
.static {
border: 4px solid transparent;
}
.absent {
border: 4px solid red;
}
.tardy {
border: 4px solid blue;
}
var $check = $("input[type=checkbox]"), el;
$check
.data('checked',0)
.click(function(e) {
el = $(this);
switch(el.data('checked')) {
// unchecked, going indeterminate
case 0:
el.data('checked',1);
el.prop('indeterminate',true);
$('img').addClass('tardy');
break;
// indeterminate, going checked
case 1:
el.data('checked',2);
el.prop('indeterminate',false);
el.prop('checked',true);
$('img').removeClass('tardy');
$('img').addClass('absent');
break;
// checked, going unchecked
default:
el.data('checked',0);
el.prop('indeterminate',false);
el.prop('checked',false);
$('img').removeClass('absent');
$('img').addClass('static');
}
});
@arvindang
Copy link
Author

Three way toggle state using a checkboxes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment