I have a javascript function
that inserts an input
of type checkbox
next to each button
. When the the first checkbox is clicked, its respective button is disabled, and the checkbox remains so when the page is refreshed, in the same way when the checkbox is clicked again the button is re-enabled and remains after page refresh.
My goal is that when the checkbox related to a button is clicked, only that button is disabled. What is happening is that only the first checkbox is triggering the function
, and when it is triggered the other checkboxes are also. When I try to click on the second or third checkbox this function doesn't work.
I assume it's because of forEach()
. What I need is to make this function work individually for each button and that keeps the checkbox enabled / disabled after page refresh.
<div id="accordion">
<% turmas.forEach((turma)=> { %>
<div class="card">
<div class="card-header" id="headingThree>">
<h5 class="mb-0 d-flex">
<div id="billing">
<input type="checkbox" id="billing-checkbox" checked>
<button class="btn btn-info text-white collapsed d-flex align-center" data-toggle="collapse" data-target="#<%= turma.nome %>" aria-expanded="false" aria-controls="<%= turma.nome %>" id="btn">
<%= turma.nome %>
</button>
</div>
</h5>
</div>
</div>
<% }) %>
$(document).ready(function() {
$('#billing-checkbox').prop('checked', localStorage.getItem('buttonDisabled') !== "disabled");
toggleBilling({
target: $('#billing-checkbox')[0]
})
$('#billing-checkbox').on('change', toggleBilling);
});
function toggleBilling(e) {
$('#billing button').attr('disabled', !e.target.checked)
localStorage.setItem('buttonDisabled', $('#billing button').attr('disabled'));
}
Update 24/06/21:
The user @RoryMcCrossan created a solution that makes each checkbox work individually for each button, but there is a problem, when the button is disabled, when trying to reactivate it, it does not enable it again on Google Chrome.
jQuery($ => {
$('.billing-checkbox').on('change', e => {
$(e.target).next('button').prop('disabled', !e.checked);
});
});
And here is the code that allows the checkbox to remain enabled/disabled after refreshing the page
$(document).ready(function() {
$('.billing-checkbox').prop('checked', localStorage.getItem('buttonDisabled') !== "disabled");
toggleBilling({
target: $('.billing-checkbox')[0]
})
$('.billing-checkbox').on('change', toggleBilling);
});
function toggleBilling(e) {
$('.billing input[type="button"]').attr('disabled', !e.target.checked)
localStorage.setItem('buttonDisabled', $('.billing input[type="button"]').attr('disabled'));
}
The ideal would be to join both functions, but I've been trying without success.