-1

I'm trying to update in "real time" if I check and uncheck in a list of checkboxs.

With this code:

window.onload = function () {
  var input = document.getElementById('listTaxi');

  function check() {
      var a = input.checked ? "checked" : "not checked";
      console.log(a);
  }
  input.onchange = check;
  check();
}

I can do this for one checkbox, but how can I make for multiple checkboxs? A list(div) of checkboxs?

Thanks!!

3
  • 1
    Use a class instead of id for all the checkbox. So that you can use document.getElementsByTagName. Commented May 26, 2016 at 17:09
  • 1
    Your code doesn't use jquery, but you tagged it as such. Do you want a jquery solution? Commented May 26, 2016 at 17:10
  • @JoseHermosillaRodrigo For each checkbox? Commented May 26, 2016 at 17:12

2 Answers 2

1

Assign a class on all checkboxes you want to check if checked or not.

Checkboxes

<input type="checkbox" class="checkboxes" id="checkbox1"/>
<input type="checkbox" class="checkboxes" id="checkbox2"/>
<input type="checkbox" class="checkboxes" id="checkbox3"/>
<input type="checkbox" class="checkboxes" id="checkbox4"/>

Pure Javascript

// getting all checkboxes
var checkboxes = document.getElementsByClassName('checkboxes');

// go through all checkboxes
for(var i = 0; i <= checkboxes.length - 1; i++){
  checkboxes[i].onchange = function(e){
    alert('Element with id ' + e.target.getAttribute('id') + ' is checked ' +e.target.checked);
  }
}

Codepen http://codepen.io/todorutandrei/pen/rLBQOX

Or you can use JQUERY - is it more simple

$('.checkboxes').change(function(){
  var item = $(this);
  alert('Element with id ' + item.attr('id') + ' is ' + item.is(':checked'));
})

Codepen http://codepen.io/todorutandrei/pen/MegzwR

Sign up to request clarification or add additional context in comments.

6 Comments

stackoverflow.com/questions/13330202/… I use this to create dynamically the checkboxes, I try to put : checkbox.class="checkboxes" but if I try your Jquery not works.
do you want to append checkboxes on DOM or to check a list of checkboxes is checked or not ?
I want to display in a page and check
i dont understand ... the checkboxes are added by javascript ? How do you want handle the check ? It's not by a simple click on checkbox ?
Yes but I need to add the checkbox list in javascript (not need this way, it can be another way) . and it's a simple click in multiple checkboxes for example.
|
0

make them all the same class or give the all the same custom attribute

$(".classname") $("input[name='customName'])

Jquery will then select all with those

$("#id").change(function() {//if using class name or custom attr loop through the return elements and use a function below to handle the cases
 if($(this).is(":checked")) {
//code if checked
}
else{
//code if not checked       
}
});

3 Comments

I create the list dynamically in javascript. How can I add the classname to all ?
I click in the checkboxes and I get "unchecked", "unchecked", "unchecked" and never "checked"
are you building the html for the list dynamically just add class="className" inside the tag when you build it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.