Shilft select multiple checkboxes in a tableWhat is the best way to add options to a select from as a JS object with jQuery?Add table row in jQuerySetting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?How can I select an element by name with jQuery?Get selected text from a drop-down list (select box) using jQueryCheck if checkbox is checked with jQuery

Why linear maps act like matrix multiplication?

Relation between Frobenius, spectral norm and sum of maxima

If two metric spaces are topologically equivalent (homeomorphic) imply that they are complete?

What are these boxed doors outside store fronts in New York?

Mathematical cryptic clues

Theorems that impeded progress

How to get entity id(order id) in success phtml

is it possible to make sharp wind that can cut stuff from afar?

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

"which" command doesn't work / path of Safari?

Creating a document with mixed languages

How to say job offer in Mandarin/Cantonese?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Does the fruit of Mantra Japa automatically go to Indra if Japa Samarpana Mantra is not chanted?

Why can't I see bouncing of a switch on an oscilloscope?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

Can I make popcorn with any corn?

same font throughout bibliography

Prevent a directory in /tmp from being deleted

Shell script not opening as desktop application

Why don't electromagnetic waves interact with each other?

Do I have a twin with permutated remainders?

How do you know if an analog film camera is still working?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?



Shilft select multiple checkboxes in a table


What is the best way to add options to a select from as a JS object with jQuery?Add table row in jQuerySetting “checked” for a checkbox with jQuery?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How can I select an element with multiple classes in jQuery?Get selected value in dropdown list using JavaScript?How can I select an element by name with jQuery?Get selected text from a drop-down list (select box) using jQueryCheck if checkbox is checked with jQuery






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I found a solution to select a single range of checkboxes in table created by JsGrid.



 $(document).on('click', '[type = checkbox]' , function(e) 
if (e.shiftKey)
var first = $("#jsGrid input[type='checkbox']:checked").first().parent().parent().index(),
last = $("#jsGrid input[type='checkbox']:checked").last().parent().parent().index();

for (var i = first; i < last; i++)
$("#jsGrid").find("tbody tr index").eq(i).children().first().find("input[type='checkbox']").prop("checked", true);

$("#jsGrid .jsgrid-grid-body tbody tr").eq(i).find("input[type='checkbox']").prop("checked", true)


);


The problem with this solution is that is only allows for a single range of checkboxes.



To add multiple ranges, I can set the bottom of the range to the current row index with



last = $(this).parent().parent().index();


Now I would like to find the row index of the row with next row above that has a selected checkbox.



I would think it should be a variant of the code below.



first = $(this).prevUntil(':checkbox', 'checked').parent().parent().index(); // -1


I can't get this to work.
Or this



 first = $(this).parent().parent().prevUntil(':checkbox', 'checked').index(); // -1


HTML



<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>


Thanks!



shift select










share|improve this question
























  • While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

    – Mohamed-Yousef
    Mar 9 at 3:24












  • I added the rendered html

    – Caldera500
    Mar 9 at 4:15











  • So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

    – Mohamed-Yousef
    Mar 9 at 4:42












  • No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

    – Caldera500
    Mar 9 at 12:26











  • Caldera500 .. answer updated with the prevUntil()

    – Mohamed-Yousef
    Mar 9 at 19:42


















0















I found a solution to select a single range of checkboxes in table created by JsGrid.



 $(document).on('click', '[type = checkbox]' , function(e) 
if (e.shiftKey)
var first = $("#jsGrid input[type='checkbox']:checked").first().parent().parent().index(),
last = $("#jsGrid input[type='checkbox']:checked").last().parent().parent().index();

for (var i = first; i < last; i++)
$("#jsGrid").find("tbody tr index").eq(i).children().first().find("input[type='checkbox']").prop("checked", true);

$("#jsGrid .jsgrid-grid-body tbody tr").eq(i).find("input[type='checkbox']").prop("checked", true)


);


The problem with this solution is that is only allows for a single range of checkboxes.



To add multiple ranges, I can set the bottom of the range to the current row index with



last = $(this).parent().parent().index();


Now I would like to find the row index of the row with next row above that has a selected checkbox.



I would think it should be a variant of the code below.



first = $(this).prevUntil(':checkbox', 'checked').parent().parent().index(); // -1


I can't get this to work.
Or this



 first = $(this).parent().parent().prevUntil(':checkbox', 'checked').index(); // -1


HTML



<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>


Thanks!



shift select










share|improve this question
























  • While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

    – Mohamed-Yousef
    Mar 9 at 3:24












  • I added the rendered html

    – Caldera500
    Mar 9 at 4:15











  • So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

    – Mohamed-Yousef
    Mar 9 at 4:42












  • No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

    – Caldera500
    Mar 9 at 12:26











  • Caldera500 .. answer updated with the prevUntil()

    – Mohamed-Yousef
    Mar 9 at 19:42














0












0








0








I found a solution to select a single range of checkboxes in table created by JsGrid.



 $(document).on('click', '[type = checkbox]' , function(e) 
if (e.shiftKey)
var first = $("#jsGrid input[type='checkbox']:checked").first().parent().parent().index(),
last = $("#jsGrid input[type='checkbox']:checked").last().parent().parent().index();

for (var i = first; i < last; i++)
$("#jsGrid").find("tbody tr index").eq(i).children().first().find("input[type='checkbox']").prop("checked", true);

$("#jsGrid .jsgrid-grid-body tbody tr").eq(i).find("input[type='checkbox']").prop("checked", true)


);


The problem with this solution is that is only allows for a single range of checkboxes.



To add multiple ranges, I can set the bottom of the range to the current row index with



last = $(this).parent().parent().index();


Now I would like to find the row index of the row with next row above that has a selected checkbox.



I would think it should be a variant of the code below.



first = $(this).prevUntil(':checkbox', 'checked').parent().parent().index(); // -1


I can't get this to work.
Or this



 first = $(this).parent().parent().prevUntil(':checkbox', 'checked').index(); // -1


HTML



<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>


Thanks!



shift select










share|improve this question
















I found a solution to select a single range of checkboxes in table created by JsGrid.



 $(document).on('click', '[type = checkbox]' , function(e) 
if (e.shiftKey)
var first = $("#jsGrid input[type='checkbox']:checked").first().parent().parent().index(),
last = $("#jsGrid input[type='checkbox']:checked").last().parent().parent().index();

for (var i = first; i < last; i++)
$("#jsGrid").find("tbody tr index").eq(i).children().first().find("input[type='checkbox']").prop("checked", true);

$("#jsGrid .jsgrid-grid-body tbody tr").eq(i).find("input[type='checkbox']").prop("checked", true)


);


The problem with this solution is that is only allows for a single range of checkboxes.



To add multiple ranges, I can set the bottom of the range to the current row index with



last = $(this).parent().parent().index();


Now I would like to find the row index of the row with next row above that has a selected checkbox.



I would think it should be a variant of the code below.



first = $(this).prevUntil(':checkbox', 'checked').parent().parent().index(); // -1


I can't get this to work.
Or this



 first = $(this).parent().parent().prevUntil(':checkbox', 'checked').index(); // -1


HTML



<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>


Thanks!



shift select







javascript jquery dom






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 12:27







Caldera500

















asked Mar 9 at 3:15









Caldera500Caldera500

315




315












  • While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

    – Mohamed-Yousef
    Mar 9 at 3:24












  • I added the rendered html

    – Caldera500
    Mar 9 at 4:15











  • So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

    – Mohamed-Yousef
    Mar 9 at 4:42












  • No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

    – Caldera500
    Mar 9 at 12:26











  • Caldera500 .. answer updated with the prevUntil()

    – Mohamed-Yousef
    Mar 9 at 19:42


















  • While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

    – Mohamed-Yousef
    Mar 9 at 3:24












  • I added the rendered html

    – Caldera500
    Mar 9 at 4:15











  • So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

    – Mohamed-Yousef
    Mar 9 at 4:42












  • No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

    – Caldera500
    Mar 9 at 12:26











  • Caldera500 .. answer updated with the prevUntil()

    – Mohamed-Yousef
    Mar 9 at 19:42

















While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

– Mohamed-Yousef
Mar 9 at 3:24






While we talk here about find the element .. It will be better to show your rendered Html code not the js code while you don't have any issue with js

– Mohamed-Yousef
Mar 9 at 3:24














I added the rendered html

– Caldera500
Mar 9 at 4:15





I added the rendered html

– Caldera500
Mar 9 at 4:15













So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

– Mohamed-Yousef
Mar 9 at 4:42






So you need to get all the row indexs which has a checked checkbox? or all the rows has checked checkboxes before the clicked one?? or just check the previous row it has a checked checkbox or not?

– Mohamed-Yousef
Mar 9 at 4:42














No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

– Caldera500
Mar 9 at 12:26





No. I want to find a single index of the closest checked box above in the table of the box that is shift-selected. The point of this to check all checkbox between 2 (first and last) boxes when I shift-select the lower box. I thought I could use prevUntil to traverse the table up over the unchecked boxes until it get the next checked one.

– Caldera500
Mar 9 at 12:26













Caldera500 .. answer updated with the prevUntil()

– Mohamed-Yousef
Mar 9 at 19:42






Caldera500 .. answer updated with the prevUntil()

– Mohamed-Yousef
Mar 9 at 19:42













1 Answer
1






active

oldest

votes


















0














[1] What I understand is: When checked the last checkbox get the nearest previous row which has checked checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





Explanation:- with checkbox use change event.



if(this.checked) // if this checkbox checked
var ThisRowIndex = $(this).closest('tr').index(); // get the closest ow index when this checkbox checked
$('table tr:lt('+ (ThisRowIndex) +')').filter(function() // use `lt()` to select the previous rows then filter them to check if there is a checked checkbox in each one or not
return $(this).find('input[type="checkbox"]:checked').length; //check if there is a checked checkbox in each one or not
).each(function() // loop through the rows which has checked checkbox
console.log('This Row With Index: '+ $(this).index() + ' Has checked checkbox'); // print the index of each row
);



  • For reverse you can use $($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()) .. see updated snippet


  • :last for the last element


  • return false; in .each() break the loop


Note With the above piece of code you can do a lot of things
.. Just understand the idea of it and it will be very helpful for
you





[2] And with prevUntil() to get the indexes of the rows between index checked checkbox and the shift-select checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>








share|improve this answer

























  • Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

    – Caldera500
    Mar 10 at 22:01












  • No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

    – Mohamed-Yousef
    Mar 10 at 22:18











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073647%2fshilft-select-multiple-checkboxes-in-a-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














[1] What I understand is: When checked the last checkbox get the nearest previous row which has checked checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





Explanation:- with checkbox use change event.



if(this.checked) // if this checkbox checked
var ThisRowIndex = $(this).closest('tr').index(); // get the closest ow index when this checkbox checked
$('table tr:lt('+ (ThisRowIndex) +')').filter(function() // use `lt()` to select the previous rows then filter them to check if there is a checked checkbox in each one or not
return $(this).find('input[type="checkbox"]:checked').length; //check if there is a checked checkbox in each one or not
).each(function() // loop through the rows which has checked checkbox
console.log('This Row With Index: '+ $(this).index() + ' Has checked checkbox'); // print the index of each row
);



  • For reverse you can use $($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()) .. see updated snippet


  • :last for the last element


  • return false; in .each() break the loop


Note With the above piece of code you can do a lot of things
.. Just understand the idea of it and it will be very helpful for
you





[2] And with prevUntil() to get the indexes of the rows between index checked checkbox and the shift-select checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>








share|improve this answer

























  • Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

    – Caldera500
    Mar 10 at 22:01












  • No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

    – Mohamed-Yousef
    Mar 10 at 22:18















0














[1] What I understand is: When checked the last checkbox get the nearest previous row which has checked checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





Explanation:- with checkbox use change event.



if(this.checked) // if this checkbox checked
var ThisRowIndex = $(this).closest('tr').index(); // get the closest ow index when this checkbox checked
$('table tr:lt('+ (ThisRowIndex) +')').filter(function() // use `lt()` to select the previous rows then filter them to check if there is a checked checkbox in each one or not
return $(this).find('input[type="checkbox"]:checked').length; //check if there is a checked checkbox in each one or not
).each(function() // loop through the rows which has checked checkbox
console.log('This Row With Index: '+ $(this).index() + ' Has checked checkbox'); // print the index of each row
);



  • For reverse you can use $($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()) .. see updated snippet


  • :last for the last element


  • return false; in .each() break the loop


Note With the above piece of code you can do a lot of things
.. Just understand the idea of it and it will be very helpful for
you





[2] And with prevUntil() to get the indexes of the rows between index checked checkbox and the shift-select checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>








share|improve this answer

























  • Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

    – Caldera500
    Mar 10 at 22:01












  • No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

    – Mohamed-Yousef
    Mar 10 at 22:18













0












0








0







[1] What I understand is: When checked the last checkbox get the nearest previous row which has checked checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





Explanation:- with checkbox use change event.



if(this.checked) // if this checkbox checked
var ThisRowIndex = $(this).closest('tr').index(); // get the closest ow index when this checkbox checked
$('table tr:lt('+ (ThisRowIndex) +')').filter(function() // use `lt()` to select the previous rows then filter them to check if there is a checked checkbox in each one or not
return $(this).find('input[type="checkbox"]:checked').length; //check if there is a checked checkbox in each one or not
).each(function() // loop through the rows which has checked checkbox
console.log('This Row With Index: '+ $(this).index() + ' Has checked checkbox'); // print the index of each row
);



  • For reverse you can use $($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()) .. see updated snippet


  • :last for the last element


  • return false; in .each() break the loop


Note With the above piece of code you can do a lot of things
.. Just understand the idea of it and it will be very helpful for
you





[2] And with prevUntil() to get the indexes of the rows between index checked checkbox and the shift-select checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>








share|improve this answer















[1] What I understand is: When checked the last checkbox get the nearest previous row which has checked checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





Explanation:- with checkbox use change event.



if(this.checked) // if this checkbox checked
var ThisRowIndex = $(this).closest('tr').index(); // get the closest ow index when this checkbox checked
$('table tr:lt('+ (ThisRowIndex) +')').filter(function() // use `lt()` to select the previous rows then filter them to check if there is a checked checkbox in each one or not
return $(this).find('input[type="checkbox"]:checked').length; //check if there is a checked checkbox in each one or not
).each(function() // loop through the rows which has checked checkbox
console.log('This Row With Index: '+ $(this).index() + ' Has checked checkbox'); // print the index of each row
);



  • For reverse you can use $($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()) .. see updated snippet


  • :last for the last element


  • return false; in .each() break the loop


Note With the above piece of code you can do a lot of things
.. Just understand the idea of it and it will be very helpful for
you





[2] And with prevUntil() to get the indexes of the rows between index checked checkbox and the shift-select checkbox






$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>








$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
var ThisRowIndex = $(this).closest('tr').index();
$($('table tr:lt('+ (ThisRowIndex) +')').get().reverse()).filter(function()
return $(this).find('input[type="checkbox"]:checked').length;
).each(function()
console.log('This Nearst Row Index: '+ $(this).index() + ' Has checked checkbox');
return false;
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>





$('input[type="checkbox"]:last').on('change' , function()
if(this.checked)
$(this).closest('tr').prevUntil('tr:has(input[type="checkbox"]:checked)').each(function()
console.log($(this).index());
);

);

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" value="100" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-row">
<td>1</td>
<td><input type="checkbox" rel="898"/></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
<tr class="jsgrid-alt-row">
<td>1</td>
<td><input type="checkbox" rel="898"></td>
<td>02/27/2019</td>
<td>19122-37</td>
<td>SMITH</td>
</tr>
</tbody>
</table>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 19:54

























answered Mar 9 at 5:14









Mohamed-YousefMohamed-Yousef

19.9k31224




19.9k31224












  • Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

    – Caldera500
    Mar 10 at 22:01












  • No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

    – Mohamed-Yousef
    Mar 10 at 22:18

















  • Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

    – Caldera500
    Mar 10 at 22:01












  • No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

    – Mohamed-Yousef
    Mar 10 at 22:18
















Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

– Caldera500
Mar 10 at 22:01






Great. Thank You! Your answer helped me to refactor the code and avoid even dealing with row index. The code below will set the checkboxes. $(this).children().find("input[type='checkbox']").prop("checked", true);

– Caldera500
Mar 10 at 22:01














No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

– Mohamed-Yousef
Mar 10 at 22:18





No need for .children() you can directly use $(this).find .. Have a great day :-) @Caldera500

– Mohamed-Yousef
Mar 10 at 22:18



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073647%2fshilft-select-multiple-checkboxes-in-a-table%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form