Check a checkbox with javascript

0

I receive information from an arrangement and I want that according to what I receive, the check that I have is activated, I have used several options and none of them works for me:

$("#inicio").prop("checked", true);
$('input[name="inicio"][value=' + info.inicioMes + ']').prop("checked", true);
document.getElementById("inicio").disabled = false;
$('input[name="inicio"]').prop("checked", true);
$("#inicio").attr("checked", "checked");

everything in javascript, thanks for your help.

    
asked by Irais_Rivero 05.01.2018 в 18:23
source

1 answer

0

I hope this can help you:

window.onload = () => {
  const data = ['Boat', 'Car', 'Truck'];
  const checkbox = document.querySelectorAll('[name="vehicle"]');
  checkbox.forEach(element => {
    let inData = data.findIndex(el => el === element.value);
    if (inData !== -1) {
      element.checked = true;
    }
  });
}
<input type="checkbox" name="vehicle" value="Boat"> I have a boat<br>
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"> I have a car<br>
    
answered by 05.01.2018 в 18:52