user3765601 user3765601 - 3 years ago 76
HTML Question

Check-box - how to get the data in a row if the checkbox in a row is checked?

I want to make a list of medicines and check all the medicines that I want to buy. I've thought of using a checkbox to check all the medicines that I wish to order then transfer the name of the medicine to the Javascript once the order button is clicked.

I'm quite new to Javascript and jQuery so I'm not sure how to make this work. I've searched other codes but I can't find any that can be related to my problem.

Here is the table code I made:

<table id="example1" >
<thead>
<tr>
<th></th>
<th>Brand Name</th>
<th>Medicine Name </th>
<th>Price</th>


</tr>
</thead>
<tbody>


<tr>
<td style="width: 5px"><input type="checkbox" /></td>
<td>Xeloda</td>
<td> Capecitabine</td>
<td>$150 </td>
</tr>
<tr>
<td style="width: 5px"><input type="checkbox" /></td>
<td>Camptosar</td>
<td>Irinotecan Hydrochloride</td>
<td>$200 </td>
</tr>
<tr>
<td style="width: 5px"><input type="checkbox" /></td>
<td>Eloxatin</td>
<td>Oxaliplatin</td>
<td>$150 </td>
</tr>




</table>
<input type="button" value="order" onclick="getAllMedicineNameOfCheckedBox()"/>

Answer Source
$(":checked").closest("tr").children("td").get()

will return an array of all the td elements in a row. You will want to do some additional code, to handle multiple checkboxes, etc and maybe tie it to click handlers.

$("input:checkbox").on("click", function(){
  if ($(this).is(":checked")){
      var data = $(this).closest("tr").children("td").get();

      /*  loop over TD items.  */
  } 
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download