1

I have this fiddle : http://jsfiddle.net/qc4uje71/16/

A list is created with a checkbox. I want to get the name of checked items on button click.

I have the index of checked items with

alert(i).

How can I get the name of checked item (ie : item0, item1..) ?

5 Answers5

3

You can use nextSibling with nodeValue:

  for (var i = 0; i < 5; i++) {

    var node = document.createElement("li");
    var x = document.createElement("INPUT");
    x.setAttribute("type", "checkbox");
    x.id = "in" + i;
    node.appendChild(x);
    var textnode = document.createTextNode("item" + i);
    node.appendChild(textnode);
    document.getElementById("myList").appendChild(node);
  }

  function displayCheckedGroup() {
    for (var i = 0; i < 5; i++) {
      if (document.getElementById("in" + i).checked == true) {
        //change this
        alert(document.getElementById("in" + i).nextSibling.nodeValue);
      }
    }
  }
ul {
  list-style: none;
  padding: 0px;
  margin: 0px;
}
li {
  clear: left;
  font-size: 25px;
  padding: 3px 0px 3px 10px;
  margin: 0px;
}
li input {
  float: right;
  margin-right: 15px;
  width: 25px;
  height: 25px;
}
li:nth-child(even) {
  background-color: #eee;
}
<ul id="myList">
</ul>

<button onclick="displayCheckedGroup()">Display checked items</button>

Also you can change a bit your function:

  function displayCheckedGroup() {
      var elem, i;
      for (i = 0; i < 5; i++) {
          elem = document.getElementById("in" + i)
          if (elem.checked) {
              alert(elem.nextSibling.nodeValue);
          }
      }
  }

References

Node.nextSibling

Node.nodeValue

Alex Char
  • 32,879
  • 9
  • 49
  • 70
0
document.getElementById("in"+i).parentNode.textContent;

You can use textContent which has a wider browser support than innerText.

Samurai
  • 3,724
  • 5
  • 27
  • 39
  • `innerText` isn't available in many browsers: http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – Paul Roub May 28 '15 at 15:34
0

You can use document.querySelectorAll('input[type=checkbox]:checked') to get the checked items, then do whatever you need to do with them.

  for(var i = 0 ; i<5; i++){

 var node = document.createElement("li");
 var x = document.createElement("INPUT");
    x.setAttribute("type", "checkbox"); 
    x.id="in"+i;
    node.appendChild(x);
 var textnode = document.createTextNode("item"+i);
 node.appendChild(textnode);
 document.getElementById("myList").appendChild(node);
  }

function displayCheckedGroup(){
    var items = document.querySelectorAll('input[type=checkbox]:checked');
    for(var i = 0; i < items.length; i++ ){
        alert(items[i].id);
    }
}
 <ul id="myList">
 </ul>

 <button onclick="displayCheckedGroup()">Display checked items</button> 
ray
  • 26,557
  • 5
  • 28
  • 27
0

You don't specify a browser so here's a version that uses the highly useful querySelectorAll function:

var list = [].slice.call(document.querySelectorAll('input[type="checkbox"]'))
               .filter(function(input) {
                   return input.checked;
               });

this will give you an array of checked checkboxes. Adding this line:

list.forEach(function(input) {alert(input.value);});

will alert each item's value.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
0

If the name is a simple name you can give the input element that name. You produce <input type="checkbox" name="item0" id="in0"> and get it with document.getElementById(...).name. On the other hand, if name is really a text and can be anything then you really need to get the text after the input with document.getElementById(...).nextSibling.wholeText (or similar functions).

m4ktub
  • 3,061
  • 1
  • 13
  • 17