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

javascript - Get checked checkboxes inside an element - Stack Overflow

programmeradmin2浏览0评论

i have a <div id="myDiv"> containing a few <input type="checkbox"> and <br/>. Now i want to loop through all elements of the div and do something if the element is a checkbox and checked. i have already tried

var e = document.getElementById("myDiv");
var c;
for(c in e.children){
    if(c.checked) {
        //...
    }
}

but c.checked is always undefined. Can anyone tell me how i do that?

i have a <div id="myDiv"> containing a few <input type="checkbox"> and <br/>. Now i want to loop through all elements of the div and do something if the element is a checkbox and checked. i have already tried

var e = document.getElementById("myDiv");
var c;
for(c in e.children){
    if(c.checked) {
        //...
    }
}

but c.checked is always undefined. Can anyone tell me how i do that?

Share Improve this question edited Feb 8, 2018 at 15:50 Roko C. Buljan 207k41 gold badges328 silver badges340 bronze badges asked Feb 8, 2018 at 15:22 GinsoGinso 5692 gold badges21 silver badges36 bronze badges 5
  • Jquery is allowed? – Kannan T Commented Feb 8, 2018 at 15:24
  • Why don't you actually select the checkboxes? – epascarello Commented Feb 8, 2018 at 15:24
  • 1 @kannan Do you see JQuery tagged in the question? – Scott Marcus Commented Feb 8, 2018 at 15:24
  • Please post the acpanying HTML. – Scott Marcus Commented Feb 8, 2018 at 15:25
  • 1 document.querySelectorAll('#myDiv > input') should help you get started. Grab all inputs under #myDiv – Sterling Archer Commented Feb 8, 2018 at 15:25
Add a ment  | 

6 Answers 6

Reset to default 3

You can go directly and target your :checked ones using:

const ckbChecked = document.querySelectorAll("#myDiv input[type=checkbox]:checked");

or one by one and than figuring out what to do with every respective one depending on the boolean Element.checked property state:

const ckb = document.querySelectorAll("#myDiv input[type=checkbox]");

[...ckb].forEach( el => {

  if( el.checked ) {
    // Is checked!
    console.log( el.value )
    el.closest("label").style.background = "gold";
  } else {
    // Not checked one
    // ... do something else
    el.closest("label").style.background = "gray";
  }

});
<div id="myDiv">
  <label><input type="checkbox" value="foo"> Foo</label>
  <label><input type="checkbox" value="bar" checked> Bar</label>
  <label><input type="checkbox" value="baz"> Baz</label>
</div>

Actually select the checkboxes. Old way would be to select the inputs and check to see if they are checked, modern way, just use a selector to get the checked ones.

var oldWay = document.getElementById("myDiv").getElementsByTagName("input");
for (var i=0; i<oldWay.length; i++) {
  console.log(oldWay[i].checked)
}

var modernWay = document.querySelectorAll("#myDiv input:checked")
console.log(modernWay)
<div id="myDiv">
  <input type="checkbox" checked="checked" />  
  <input type="checkbox" checked="checked" />
  <input type="checkbox" />
  <input type="checkbox" />  
</div>

let checkboxes = [...document.querySelectorAll('input[type=checkbox]')]

checkboxes.forEach(checkbox => {
  if(checkbox.checked) {
    console.log('i am checked', checkbox)
  }
})
<div>
  <input type="checkbox" checked/>
  <input type="checkbox" />
  <input type="checkbox" checked/>
  <input type="checkbox" />
</div>

Use the below

var matches = [];
var searchEles = document.getElementById("myDiv").children;
for(var i = 0; i < searchEles.length; i++) {
  var elem = searchEles[i];
  
  if (elem.type == "checkbox" && elem.checked) {
    console.log(elem.id + " is Checked");
  }
}
<div id="myDiv">
  <input type="checkbox" id="chk1">
  <input type="checkbox" id="chk2" checked="checked">
  <input type="checkbox" id="chk3">
  <input type="checkbox" id="chk4" checked="checked">
</div>

c in your for in loop is the index of the array, that is why the checked property got undefined when you tried to access it.

Below code will work for you:

<div id="my-div">
  <input type="checkbox" />
  <input type="checkbox" checked="checked" />
  <input type="checkbox" />
  <input type="checkbox" />
</div>

JS:

let myDiv = document.getElementById('my-div');

for (let i = 0; i < myDiv.children.length; i++) {
  console.log(myDiv.children[i]);
  if (myDiv.children[i].nodeName.toLowerCase() === 'input') {
    console.log(myDiv.children[i].checked);
  }
}

Your code was actually really close anyway. It should have been of as opposed to in.

var e = document.getElementById("myDiv");
var c;

for (c of e.children) {
  if (c.checked) {
    console.log("winning")
  }
}
<div id="myDiv">
  <input type="checkbox" />
  <input type="checkbox" checked />
  <input type="checkbox" checked />
  <input type="checkbox" />
  <input type="checkbox" checked />
</div>

Hope this helps

发布评论

评论列表(0)

  1. 暂无评论