Here is a code snippet that includes buttons appending selections to the "cart" div upon clicking.
The first script ensures only one selection per row when multiple buttons in the same row are clicked.
In the second script, selections are appended to the "cart" div and removed if they have been unselected.
An issue arises when clicking on multiple buttons in the same row. The first script works as intended, deactivating the previous selection and activating the current button. However, the second script is meant to update the new selection for the active button and remove the previous one in the same row.
How can I amend the second script to address this functionality?
<body style="margin:10px;">
<div class="cart">
<div id="box" class="boxlit"></div><br>
</div>
<table id="Table1" class="Fixtures-Table">
<thead class="disnon">
<tr>
<th>League</th>
<th>Home [1]</th>
<th>Draw [x]</th>
<th>Away [2]</th>
<th>Kickoff</th>
</tr>
</thead>
<tr class="items" style="display: table-row;">
<td class="addItem">Almagro-Temperley</td>
<td id="bbox"><input type="button" class="decimals" id="3" value="1.95" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="4" value="2.95" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="5" value="3.90" data-selected="false"></td>
<td id="date">8/1/2022 8:00:00 PM</td>
</tr>
<tr class="items" style="display: table-row;">
<td class="addItem">Guillermo Brown-Santamarina</td>
<td id="bbox"><input type="button" class="decimals" id="6" value="1.65" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="7" value="3.25" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="8" value="5.10" data-selected="false"></td>
<td id="date">8/1/2022 8:00:00 PM</td>
</tr>
<tr class="items" style="display: table-row;">
<td class="addItem">Nueva Chicago-CSD Flandria</td>
<td id="bbox"><input type="button" class="decimals" id="9" value="2.25" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="10" value="2.85" data-selected="false"></td>
<td id="bbox"><input type="button" class="decimals" id="11" value="3.15" data-selected="false"></td>
<td id="date">8/1/2022 8:00:00 PM</td>
</tr>
</table>
</body>
</html>
The first script allows one selection per row upon clicking multiple buttons within the same row.
<script>
$(document).ready(function(){
$('.decimals').click(function() {
if ($(this).attr('data-selected') === 'true') {
$(this).attr('data-selected', 'false');
$(this).removeClass('active');
} else {
$(this).closest('tr').find('.decimals').not(this)
.removeClass('active').attr('data-selected', 'false');
$(this).attr('data-selected', 'true');
$(this).addClass('active');
}
});
});
</script>
The second script appends selections to the "cart" div and updates them when choices change or get deselected.
It should replace the previous selection in the same row with the current active button's choice.
<script>
let $th = $('#Table1 thead th');
$(".decimals").on('click', e => {
let $btn = $(e.target);
let $option = $(`.box[data-id="${$btn.prop('id')}"]`);
let $tr = $btn.closest('tr');
let selectionIndex = $btn.closest('td').index();
let kickoff = $tr.find('td:last').text().trim();
let match = $tr.find('td:first').text().trim();
let result = $th.eq(selectionIndex).text().trim();
let value = $btn.val();
if ($option.length){
$option.remove();
return;
}
$("#box").append(`<div class="box" data-id="${$btn.prop('id')}">${match}<br>${result}<div class="crtTotal">${value}</div><input type="hidden" name="kickoff[]" value="${kickoff}"/><input type="hidden" name="match[]" value="${match}"/><input type="hidden" name="result[]" value="${result}" readonly/><input type="hidden" name="value[]" value="${value}"/></div>`);
});
</script>
.decimals {
background-color: rgb(31, 31, 31);
color: rgb(255, 255, 255);
border: 2px solid #0e1516;
border-radius: 4px;
font-size: 13px;
padding: 4px 10px;
font-weight: bold;
border-width: 1px;
cursor: pointer;
}
.decimals:hover {
background: rgb(51, 51, 51);
}
.active,
.active:hover {
background: rgb(161, 0, 0);
color: rgb(255, 255, 255);
border-color: rgb(156, 0, 0);
}