0

I have inputs:

  <div id="my">
   <input type="checkbox" value="1" name="names[]">
   <input type="checkbox" value="2" name="names[]">
   <input type="checkbox" value="3" name="names[]">
   <input type="checkbox" value="4" name="names[]">
  </div>

And my javascript:

initValues=[1,2,3];
$('#my').find(':checkbox[name="names[]"]').each(function () {
  $(this).prop("checked", ($.inArray($(this).val(), initValues)));
});

And now all my checkboxes are checked. How must I change my code to set checked for ckeckboxes which values are in initValues array?

4 Answers 4

3

$.inArray returns the index, not boolean. Also, parseInt your value because its considered as string when you pick it up.

initValues=[1,2,3];
            $('#my').find(':checkbox[name="names[]"]').each(function () {
              $(this).prop("checked", $.inArray(parseInt($(this).val()), initValues) == -1 ? false : true );
            });
Sign up to request clarification or add additional context in comments.

2 Comments

You can always accept/upvote correct answer to your question by clicking that hollow green tick.
Waaaay faster than me, Mehul. Well done. +1 BTW, loved your Chrome Extender tuts - thanks for those.
1

let initValues = [1, 2, 3];
$('#my').find(':checkbox[name="names[]"]').each(function() {
  if (initValues.some(v => v == $(this).val())) {
    $(this).prop('checked', true);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="my">
  <input type="checkbox" value="1" name="names[]">
  <input type="checkbox" value="2" name="names[]">
  <input type="checkbox" value="3" name="names[]">
  <input type="checkbox" value="4" name="names[]">
</div>

2 Comments

I’ve upvoted the answer below because the user explained their changes, not just provided the code, so I believed the answer is better. I don’t know why your answer was downvoted, though, I don’t think it’s bad.
Nothing at all wrong with your answer, and it's great to see the same problem solved in different ways! +1
1

You need to turn the value back into a number so it compares with the array value.

$.inArray(+$(this).val(), initValues))

Revised Example:

initValues=[1,2,3];
         $('#my').find(':checkbox[name="names[]"]').each(function () {
   $(this).prop("checked", ($.inArray(+$(this).val(), initValues)) != -1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
   <div id="my">
   <input type="checkbox" value="1" name="names[]">
   <input type="checkbox" value="2" name="names[]">
   <input type="checkbox" value="3" name="names[]">
   <input type="checkbox" value="4" name="names[]">
  </div>

1 Comment

Thanks! Fixed. In solving the problem, I used an IF structure. When I refactored to use the OP's original structure, I omitted the critical =-1 part. Mehul got it right from the beginning, and 9 mins faster than me!
0

function printChecked(isChecked, value) { const newProjectStages = projectstage.filter((p) => p !== value);

if (isChecked) {
  newProjectStages.push(value);
}

Setprojectstage(newProjectStages);

var items = document.getElementsByName("tr");
var selectedItems = [];
for (var i = 0; i < items.length; i++) {
  if (items[i].type == "checkbox" && items[i].checked == true)
    selectedItems.push(items[i].value);
}
console.log("selected val", selectedItems);
Setselectedprostages(selectedItems);

}

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.