Xravn Xravn - 7 months ago 15
Javascript Question

Select preceeding checkbox if the following checkbox is checked

I want to check box K and F is selected if Check box D is checked and other checkbox to be unchecked, Similarly, if the check box I is selected the preceding chekbox of I to be selected and the following to be unchecked.
My html code is:

<td><input type="checkbox" class= "knitting" name="knitting[]" value="1">K</td>
<td><input type="checkbox" class= "fmending" name="fmending[]" value="1">F</td>
<td><input type="checkbox" class= "dyeing" name="dyeing[]" value="1">D</td>
<td><input type="checkbox" class = "firon" name="firon[]" value="1">Y</td>
<td><input type="checkbox" class= "linking" name="linking[]" value="1">L</td>
<td><input type="checkbox" class="smending" name="smending[]" value="1">S</td>
<td><input type="checkbox" class="siron" name="siron[]" value="1">I</td>
<td><input type="checkbox" class="packing" name="packing[]" value="1">P</td>
<td><input type="checkbox" class="data" name="data[]" value="1">D</td>
<td><input type="checkbox" class="chalan" name="chalan[]" value="1">C</td>


And what I have done for that is :

$(".dyeing").click(function () {
$(".knitting").prop("checked", true);
$(".fmending").prop("checked", true);
$(".firon").prop("checked", false);
$(".linking").prop("checked", false);
$(".smending").prop("checked", false);
$(".siron").prop("checked", false);
$(".packing").prop("checked", false);
$(".data").prop("checked", false);
$(".chalan").prop("checked", false);
});


This looks messy, is there another cool process???

Answer

You can do it with :lt() and :gt() selectors:

$(":checkbox").on('change',function () {
   var idx=$(this).index();
   $(":checkbox:lt("+idx+")").prop('checked',true);
   $(":checkbox:gt("+idx+")").prop('checked',false);             
}); 

Check the below snippet

var idx = 0;
var chk = $('table tr :checkbox');
$("table tr :checkbox").on('change', function() {
  var index = chk.index($(this));
  if (idx < index) {
    idx = index;
    $("table tr :checkbox:lt(" + idx + ")").prop('checked', true);
    $(":checkbox:gt(" + idx + ")").prop('checked', false);
  } else if (idx == index && !$(this).prop('checked')) {

    $(":checkbox").prop('checked', false);
  } else {
    idx = chk.index($(this));
    $(":checkbox:lt(" + idx + ")").prop('checked', true);
    $(this).prop('checked', true);
    $(":checkbox:gt(" + idx + ")").prop('checked', false);
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <td>
        <input type="checkbox" class="knitting" name="knitting[]" value="1">K</td>
      <td>
        <input type="checkbox" class="fmending" name="fmending[]" value="1">F</td>
      <td>
        <input type="checkbox" class="dyeing" name="dyeing[]" value="1">D</td>
      <td>
        <input type="checkbox" class="firon" name="firon[]" value="1">Y</td>
      <td>
        <input type="checkbox" class="linking" name="linking[]" value="1">L</td>
      <td>
        <input type="checkbox" class="smending" name="smending[]" value="1">S</td>
      <td>
        <input type="checkbox" class="siron" name="siron[]" value="1">I</td>
      <td>
        <input type="checkbox" class="packing" name="packing[]" value="1">P</td>
      <td>
        <input type="checkbox" class="data" name="data[]" value="1">D</td>
      <td>
        <input type="checkbox" class="chalan" name="chalan[]" value="1">C</td>
    </tr>
  </tbody>
</table>