I have a table where each row has a checkbox and an input. The input can take a numerical value. To make things easy for the user, there is a separate input box that a user can enter a value, click a button which sets the input values of the selected rows in the table.
[ ] all | Name | Amount
[ ] | Marty | [ ]
[x] | Frank | [ ]
[ ] | Leslie | [ ]
[ 123.45 ] (Set selected to full amount)
When clicking the button "Set selected to full amount", the amount input for Frank would get set to 123.45.
html:
<table>
<thead>
<tr>
<th data-qaid='checkbox'><input type='checkbox'></th>
<th data-qaid='name'>Name</th>
<th data-qaid='amount'>Amount</th>
</tr>
</thead>
<tbody>
<tr data-qaid='datarow'>
<td data-qaid='checkbox'>
<input type='checkbox' name='selected' />
</td>
<td data-qaid='name'>
<label>Marty</label>
</td>
<td data-qaid='amount'>
<input type='number' name='amount' min='0' step='0.01' />
</td>
</tr>
...
</tbody>
</table>
<input id='fullAmount' type='number' min='0' step='0.01' />
<button id='setFull'>Set selected to full amount</button>
javascript: This is the part where I need help with. I'm not sure how to determine if the checkbox is selected.
$(document).ready(function() {
$('#setFull').onclick(function(e) {
e.preventDefault();
let fullAmount = $('#fullAmount').value();
$('tr[data-qaid="datarow"]').each(function() {
if ($(this).find('td[data-qaid="checkbox"] input').checked) {
$(this).find('td[data-qaid="amount"] input').value = $fullAmount;
}
});
});
});
I've got my attempt here: https://jsfiddle.net/cru1apo3/