toggle checkbox with javascript

前端 未结 4 1823
南旧
南旧 2021-01-27 06:28

I want to uncheck a checkbox using javascript. I have one button which just unchecks the box and works fine:

function clear() {
document.getElementById(\"check\"         


        
4条回答
  •  无人共我
    2021-01-27 06:48

    Treat "checked" as a boolean, not as a string.

    You can just invert it, as in

    element = document.getElementById("check")
    element.checked = !element.checked
    

    A more featured example:

    var $toggle = document.getElementById('toggle');
    var $checkbox = document.getElementById('checkbox');
    
    
    
    var toggle_checkbox = function() {
       $checkbox.checked = !checkbox.checked;
    }
    
    $toggle.addEventListener('click',toggle_checkbox);
    
    

提交回复
热议问题