I have list of many checkboxes and I need to display the order of them being checked next to each other.
Something like this:
[1] checkbox
checkbox
checkbox
[2] checkbox
checkbox
[3] checkbox
Order in which they are being checked does not matter, the thing is that they need to be ordered from top to bottom as showed.
I have limited options with editing the HTML, since it is dinamically rendered, and the structure looks like this:
<td>
<label>
<input type="checkbox">
"Sample text"
</label>
</td>
<td>
<label>
<input type="checkbox">
"Sample text"
</label>
</td>
<td>
<label>
<input type="checkbox">
"Sample text"
</label>
</td>
etc.
So I have tried the following:
$('input[type=checkbox]').on('change', function(){
var number = $('input[type=checkbox]:checked').length;
$('label:has(input[type=checkbox]:checked)').text(number);
});
But it ends up replacing each label
contents with 1 (not even a count).
I searched for answers here on Stackoverflow and I found the most suitable as this one:
document.querySelectorAll('td').forEach(el => {
el.innerHTML += '<span class="letter"> </span>'
})
let checkedItems=[]
document.querySelectorAll('[type=checkbox]').forEach(el => {
el.value = el.closest('label').innerText.trim()
el.addEventListener('change', e => {
let n = el.closest('label').innerText.trim();
if (!e.target.checked) checkedItems.splice(checkedItems.indexOf(n),1)
else checkedItems.push(n);
document.querySelectorAll('.letter').forEach( l=> l.innerHTML = '')
checkedItems.forEach((n,i) => {
document.querySelector(`input[value=${n}]`).closest('td').querySelector('.letter').innerHTML = i;
})
});
});
In this case I get an error caused by the input value, since it is not alphanumeric. In what way can I edit either of these in order to work? Thank you!
EDIT: I have found the problem in the last code block in my question > it was in the input[value=${n}]
, since it did not match my input values, I just did not spend that much time in dev tools to see this before asking
3
Answers
If I understood correctly that you wish to display, next to the checkbox itself, the order ( out of all checkboxes ) that a particular checkbox was checked then perhaps the following might work?
querySelectorAll
can be combined with the:checked
attribute selector to find the number of checked elements and this is then used within a newspan
element which is appeneded to thelabel
element when the delegated event listener fires achanged
event.update
per the comment by @janPfiefer, a resequencing function could be added if required to re-index the checked order should all checkboxes be checked and then some unchecked in random sequence.
Add span as a container for a number:
Then script will be:
Introduction and assumptions:
I edited the html putting the
<input>
before the<label>
and no more nested inside it as before. I did it because the label should not contain the input element and should be a distinct element and also because I needed an after element to style following the checkbox. It could be a wrong assumption if you needed the html to remain exactly the same. Anyway there’s a further attempt later that will also address the original html.So we have this:
Instead of this:
Showing table columns as rows instead:
You may simply have a css rule addressing the
<tr>
elements and turn them as flex container with a column direction:Using CSS counters:
It could be worth knowing that you could achieve the same result you are chasing with js, but using css only with css rulesets like these:
Where a counter
checked-cb
is incremented at each input checked and echoed in an::after
pseudo element inside the label following.Using the same css approach with the original html:
Since
<input>
elements are self contained they cannot host::after
elements so we are forced to style its parent because there are no other sibling elements to adopt as container like we had in the example before where I made the html a little better.Unfortunately the only css selector to get there requires the
:has()
psudo class currently still not supported on Firefox:References:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Counter_Styles/Using_CSS_counters
https://developer.mozilla.org/en-US/docs/Web/CSS/:has
Demo (w/ html improved):