I don't know if this is possible, but it's an interesting situation I came across in my project today.
A table of checkboxes. One type of checkboxes are hierarchical, which means that if one is checked, all checkboxes of the same type to the left of it are magically checked as well.
We need to send information only about the rightmost checked checkbox. Here's an example for using jQuery code to accomplish it: https://codepen.io/oriadam/pen/Yapodm
$("input").click(function() {
$(".here").removeClass("here");
$("tr").each(function(i, tr) {
$(tr)
.find(".a:has(input:checked):last input:checked")
.each(function(i, elem) {
$(elem).parent().addClass("here");
});
});
});
.here {
background: red;
}
.a input {
width: 2em;
height: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><input type="checkbox"></td>
<td class="a here"><input type="checkbox" checked></td>
<td class="a"><input type="checkbox"></td>
<td class="a"><input type="checkbox"></td>
<td><input type="checkbox" checked></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td class="a"><input type="checkbox" checked></td>
<td class="a here"><input type="checkbox" checked></td>
<td class="a"><input type="checkbox"></td>
<td><input type="checkbox" checked></td>
</tr>
<tr>
<td><input type="checkbox"></td>
<td class="a"><input type="checkbox" checked></td>
<td class="a"><input type="checkbox" checked></td>
<td class="a here"><input type="checkbox" checked></td>
<td><input type="checkbox" checked></td>
</tr>
</table>
<br><br> The challange: <br> Use a single jQuery(Sizzle) selector to get the rightmost checked checkbox of every tr, while considering only td that has class a.
Is there a way to use a single jQuery(Sizzle) selector to get the rightmost checked checkbox of every tr
, while considering only td
of class a
.