My problem is that I have found a solution for one group of checkboxes and shows the selected data in a text field in my dynamic formula.
But I think the line $('input:checkbox').change((e)
does not make sense if I want to use a different, or new, group of checkboxes.
My idea is that the two different groups of checkboxes get a unique id to handle with them.
<tr>
<td>
Entsperrcode:
</td>
<td>
<script src="inc/jquery-3.7.0.min.js"></script>
<br>
<table border="1"cellspacing="0" cellpadding="0">
<tr>
<td><center>1</center></td>
<td><center>2</center></td>
<td><center>3</center></td>
</tr>
<tr>
<td><input type="checkbox" id="entsperrcodewisch1" value="1"></td>
<td><input type="checkbox" id="entsperrcodewisch2" value="2"></td>
<td><input type="checkbox" id="entsperrcodewisch3" value="3"></td>
</tr>
<td><center>4</center></td>
<td><center>5</center></td>
<td><center>6</center></td>
</tr>
<tr>
<td><input type="checkbox" id="entsperrcodewisch4" value="4"></td>
<td><input type="checkbox" id="entsperrcodewisch5" value="5"></td>
<td><input type="checkbox" id="entsperrcodewisch6" value="6"></td>
</tr>
<tr>
<td><center>7</center></td>
<td><center>8</center></td>
<td><center>9</center></td>
</tr>
<tr>
<td><input type="checkbox" id="entsperrcodewisch7" value="7"></td>
<td><input type="checkbox" id="entsperrcodewisch8" value="8"></td>
<td><input type="checkbox" id="entsperrcodewisch9" value="9"></td>
</tr>
</table>
<input type="text" id="selected" name="entsperrcode"/><br><br>
<script>
(function() {
$('input:checkbox').change((e) => {
if ($(e.currentTarget).is(':checked')) {
var curVal = $('#selected').val();
if (curVal) {
$('#selected').val(curVal + '-' + e.currentTarget.value);
} else {
$('#selected').val(e.currentTarget.value);
}
} else {
var curVal = $('#selected').val().split('-');
var filteredVal = curVal.filter(el => el.trim() !== e.currentTarget.value)
$('#selected').val(filteredVal.join('-'));
}
});
})();
</script>
</td>
</tr>
<tr>
<td>
Beschädigungen:
</td>
<td>
<script src="inc/jquery-3.7.0.min.js"></script>
<br>
<input type="checkbox" id="beschaedingung1" value="Display"><br>
<input type="checkbox" id="beschaedingung2" value="Rückseite"><br>
<input type="checkbox" id="beschaedingung3" value="Rand"><br>
<input type="text" id="beschaedig" name="beschaedig"/><br><br>
<script>
(function() {
$('input:checkbox').change((e) => {
if ($(e.currentTarget).is(':checked')) {
var curVal = $('#beschaedig').val();
if (curVal) {
$('#beschaedig').val(curVal + '-' + e.currentTarget.value);
} else {
$('#beschaedig').val(e.currentTarget.value);
}
} else {
var curVal = $('#beschaedig').val().split('-');
var filteredVal = curVal.filter(el => el.trim() !== e.currentTarget.value)
$('#beschaedig').val(filteredVal.join('-'));
}
});
})();
</script>
</td>
</tr>
2
Answers
Here is an example with
closest
(https://api.jquery.com/closest/) andeach
(https://api.jquery.com/each/).In the case of a change, the parent element (
table
) is found. The output field is set as a data attribute in the table (data-output-id="#output1"
).The way I’d approach this is as below, with explanatory comments in the code:
JS Fiddle demo.
References:
background-image
.display
.:first-child
.gap
.grid-auto-rows
.grid-template-columns
.linear-gradient()
.order
.radial-gradient()
.padding
.padding-block
.padding-inline
.repeat()
.text-align
.var()
.Array.prototype.filter()
.Array.prototype.forEach()
.Array.prototype.map()
.document.createElement()
.document.querySelector()
.document.querySelectorAll()
.Element.append()
.Element.previousElementSibling()
.Element.remove()
.Element.querySelector()
.Element.querySelectorAll()
.EventTarget.addEventListener()
.Object.assign()
.