0

I have a form and it has 4 checkboxes. I am getting a response from the server and according to that response I need to checked that particular checkbox/s.

For example I have four checkboxes called 2100,2200,2300,2400

Then I am getting a array response from the server and array has 2200 and 2400. Then I need to checked both 2200 and 2400 checkboxes.

$.ajax({
    type: "POST",
    url: "send_db.php",
    data: {'send_array':array_send},
    success:  function(data){

    var jsonRes  = jQuery.parseJSON(data);
    var plant  = jsonRes.plant;
    var array_length = jsonRes.plant.length;

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

        document.getElementsByName("user_plant1").checked = true;

    }

    }
});

Here plant has the values.

These are my checkboxes...

<div class="form-group">
    <label class="checkbox-inline"><input type="checkbox" id="chk21" name="user_plant1" value="2100">2100</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk22" name="user_plant1" value="2200">2200</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk23" name="user_plant1" value="2300">2300</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk24" name="user_plant1" value="2400">2400</label>
</div>
1
  • getElementsByName(); returns a node list - collection/group of elements. Commented Dec 8, 2017 at 10:50

1 Answer 1

3

Try the following way:

var chkData = "2200,2400";
chkData = chkData.split(',');

var el=document.querySelectorAll('input[name=user_plant1]');
el.forEach(function(chk){
  if(chkData.includes(chk.value)){
    chk.setAttribute('checked',true);
  }
  else{
    chk.checked = false;
  }
});
<div class="form-group">
    <label class="checkbox-inline"><input type="checkbox" id="chk21" name="user_plant1" value="2100">2100</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk22" name="user_plant1" value="2200">2200</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk23" name="user_plant1" value="2300">2300</label>
    <label class="checkbox-inline"><input type="checkbox" id="chk24" name="user_plant1" value="2400">2400</label>
</div>

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

5 Comments

u saved my day, works really fine. I changed 'input[type=checkbox]' to "input[name='user_plant1']" for my need. thank you
@D.Madu, you are most welcome. I have updated the answer as well.....thanks
one another thing. if i checked 4 checkboxes from first response and again take a response with two checkboxes, other two not unchecked. I tried with if(chkData.includes(chk.value)){ chk.setAttribute('checked',true); }else{ chk.setAttribute('checked',false); } and try to flase all checkboxes at the top like this. document.getElementById("chk21").checked = false; document.getElementById("chk22").checked = false; document.getElementById("chk23").checked = false; document.getElementById("chk24").checked = false; both not working
Do you want to set the other check boxes unchecked on the second response?
yes. for example first time 2100,2200,2300,2400 come and checked all boxes, second time only come 2100,2200. but 2300 and 2400 also checked. I want to unchecked them

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.