最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

toggle checkbox with javascript - Stack Overflow

programmeradmin1浏览0评论

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").checked = "";
}

I have another button that I want to check the box if not checked and uncheck if it is. Below doesn't uncheck the box. I can can switch the if statement and does works the opposite way. What's the problem?

function switchIt() {
if (document.getElementById("check").checked !== "checked") {
  document.getElementById("check").checked = "checked";
} else {
  document.getElementById("check").checked = "";
}

Thanks!

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").checked = "";
}

I have another button that I want to check the box if not checked and uncheck if it is. Below doesn't uncheck the box. I can can switch the if statement and does works the opposite way. What's the problem?

function switchIt() {
if (document.getElementById("check").checked !== "checked") {
  document.getElementById("check").checked = "checked";
} else {
  document.getElementById("check").checked = "";
}

Thanks!

Share Improve this question edited Mar 6, 2019 at 22:09 Stuart Durning asked Mar 6, 2019 at 21:52 Stuart DurningStuart Durning 471 gold badge1 silver badge9 bronze badges 2
  • Can you post your code for the checkbox inputs please. – Lord Elrond Commented Mar 6, 2019 at 21:54
  • Possible duplicate of How to uncheck a checkbox in pure JavaScript? – A. Meshu Commented Mar 6, 2019 at 21:58
Add a ment  | 

4 Answers 4

Reset to default 4

switch is a reserved word, use another one

function switchIt() {
  var box = document.getElementById("check");
  
  if (box.checked) {
    box.checked = false;
  } else {
    box.checked = true;
  }
}

setInterval(switchIt, 1000);
<input type="checkbox" id="check" />

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);
<label>
  Checkbox:
  <input id="checkbox" type="checkbox" checked />
</label>
<button id="toggle">Toggle</button>

You need a boolean to do that.

Take a look at this pen:

https://codepen.io/anon/pen/jJyXgO

let checkbox = document.querySelectorAll('#check')[0]

setInterval(function() {
  checkbox.checked = !checkbox.checked
}, 1000)

I've never seen it done with a string "checked" before. Try with a boolean like:

function change() {
    if (document.getElementById("check").checked !== true) {
      document.getElementById("check").checked = true;
    } else {
      document.getElementById("check").checked = false;
    }
}

or easier

function change() {
    document.getElementById("check").checked = !document.getElementById("check").checked
}

Don't forget, switch is reserved, so use a different name, as I did with change()

发布评论

评论列表(0)

  1. 暂无评论