dimanche 5 juillet 2020

How do I get certain vaules of an array in js by checking the checkbox?

I'm new to javascript and kind of stuck with the problem. I want to create some checkboxes and when I select and submit some of them, I want to get the associated output from an array. In my case, I can only send some data by hard coding in the "value" attribute of the "input" element. But, I want to send the data from the array "names".

Can anyone please help me out?

function submit() {
  var names = ["zihan", "zihan", "zihan", "masud", "masud", "shakil"];
  var arrayNAME = [];
  var tblName = document.getElementById("tblName");
  var check = tblName.getElementsByTagName("input");
  for (var i = 0; i < check.length; i++) {
    if (check[i].checked) {
      arrayNAME.push(check[i].value);
    }
  }
  if (arrayNAME.length > 0) {
    alert(`Selected value: ${arrayNAME.join(",")}`);
  }
}
<table id="tblName">
  <tr>
    <td>
      <input type="checkbox" id="zahid" value="zahid">
      <label for="zahid"> Zahid </label>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" id="masud" value="masud">
      <label for="masud"> Masud </label>
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" id="jihan" value="jihan jihan jihan">
      <label for="jihan"> Jihan </label>
    </td>
  </tr>
</table>
<br>
<button onclick="submit()"> Submit </button>



Aucun commentaire:

Enregistrer un commentaire