I have input with an id and i need to add a class to the id when it is checked(true) and remove when it is false.
<input id="<?= $ec['id']?>" type="checkbox" value="<?= $ec['id']?>" class="category" onclick="handleClick(this)" >
<script>
function handleClick(cb) {
var id= cb.id;var checked = cb.checked ;
console.log("id is " + id + cb.checked);
if ($('#id').is(':checked')) {
console.log("inside if");
}
}
</script>
Use
.toggleClass()
withstate
function handleClick(cb) { $(cb).toggleClass('checked', cb.checked); }
.checked { border: solid #ff0000 5px; width: 30px; height: 30px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="checkbox" value="100" class="category" onclick="handleClick(this)">
You can try this
function handleClick(cb) {
var id= cb.id;
var checked = cb.checked ;
console.log("id is " + id + cb.checked);
if(cb.checked) {
$(cb).addClass('check');
}
else {
$(cb).removeClass('check');
}
}
function handleClick(cb)
{
$(cb).prop('checked') ? $(cb).addClass('checkboxSelected') : $(cb).removeClass('checkboxSelected');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.