JQuery button to select a check box

I have a grid with user data with the first column having a checkbox. I have a button at the top of the grid. If I click the button, I need to select the 5 best users from the list. Can someone tell me how to do this using jquery?

Thanks.

+4
source share
2 answers
$('#myButton').click(function() { $('#Grid input[type=checkbox]:lt(5)').attr('checked','checked'); }); 

If you have other checkboxes in the grid, a problem may occur. This is the best I can do without seeing your HTML.

+4
source

I found this in jQuery Doc

Because: lt () is an extension of jQuery and not part of the CSS specification, queries using: lt () cannot take advantage of the performance enhancement provided by the built-in DOM request of the SelectorAll () method. For best performance in modern browsers, use $ ("your-clean-css-selector"). slice (0, index) instead.

Based on the accepted answer, you can do this:

 $('#myButton').click(function() { $("#Grid input[type=checkbox]").slice(0,5).attr('checked','checked'); }); 
0
source

Source: https://habr.com/ru/post/1308475/


All Articles