Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

540
Views
Jquery Selecting Table Rows with Checkbox

I have a table as

<table id="rowclick2">
  <tbody>
    <tr >
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 1</td>
       <td>A</td>
    </tr>    
    <tr >
      <td class="cb"><input type="checkbox" value="yes" ></td>
      <td>row 2</td>
       <td>B</td>
    </tr>    
    <tr>
      <td class="cb"><input type="checkbox" value="yes"></td>
      <td>row 3</td>
      <td>C</td>  
    </tr>    
  </tbody>
</table>

where I want to get each cell (when the button is clicked) in a row whose checkboxes are checked

I tried filter

  $('#test').click(function(){

      $('#rowclick2 tr').filter(':has(:checkbox:checked)').each(
          //get row values
      );
    });

It's pretty simple yet I can't see what I am missing...

Here is the jsfiddle link...

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You're almost there :)

If you want all <td> s in rows where the checkbox is checked:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td');

E.g.:

$('#rowclick2 tr').filter(':has(:checkbox:checked)').find('td').each(function() {
    // this = td element
});

More elaborate example is on JsFiddle.

BTW
The .filter(':has(:checkbox:checked)') can be written as .has(':checkbox:checked') if you, like me, find that easier to read.

over 4 years ago · Santiago Trujillo Report

0

You can try this code:

$('#test').click(function(){

    $('td.cb:checked').parents('tr').each(
          //get row values
      );
}

HTH!

over 4 years ago · Santiago Trujillo Report

0

// select row and check box function condition
        $('#file_module_mainTableId').on('click', 'tbody tr', function(e){
            //e.preventDefault();
            var $this = $(this);
            // Detecting ctrl (windows) / meta (mac) key.
            if (e.ctrlKey || e.metaKey)
            {
                if($this.hasClass('ui-selected')){
                    $this.removeClass('ui-selected');
                    $this.find('input[type=checkbox]').removeAttr('checked');
                    if (e.target.type !== 'checkbox') {
                        $(':checkbox', this).trigger('click');
                    }
                }else{
                    $this.addClass('ui-selected')
                    $this.find('input[type=checkbox]').attr('checked', 'checked');
                }
            }
            // Detecting shift key
            else if (e.shiftKey)
            {
                // Get the first possible element that is selected.
                var currentSelectedIndex = $('#file_module_mainTableId tbody tr.ui-selected').eq(0).index();
                // Get the shift+click element
                var selectedElementIndex = $('#file_module_mainTableId tbody tr').index($this);
                // Mark selected between them
                if (currentSelectedIndex < selectedElementIndex)
                {
                    for (var indexOfRows = currentSelectedIndex; indexOfRows <= selectedElementIndex; indexOfRows++)
                    {
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).addClass('ui-selected');
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).find('input[type=checkbox]').attr('checked', 'checked');
                    }
                }
                else
                {
                    for (var indexOfRows = selectedElementIndex; indexOfRows <= currentSelectedIndex; indexOfRows++)
                    {
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).addClass('ui-selected');
                         $('#file_module_mainTableId tbody tr').eq(indexOfRows).find('input[type=checkbox]').attr('checked', 'checked');
                    }
                }    
            }
            else
            {
                if (e.target.type !== 'checkbox') {
                    $(':checkbox', this).trigger('click');
                }
            }
        });
        // checkbox class section  
        $(".chkbox").change(function(e){
            if ($(this).is(":checked")){
                $(this).closest('tr').addClass("ui-selected");
            } else {
                $(this).closest('tr').removeClass("ui-selected");
            }
        });
        // clrt + A select all table row 
        $(document).on('keydown', function(e){
            if(!$('body').hasClass('.modal-open')){ // use this condition for modal - deprecated here
                if(e.metaKey || e.ctrlKey && e.keyCode == 65){
                    $('#file_module_mainTableId tbody tr').addClass('ui-selected');
                    $('#file_module_mainTableId tbody tr').find('input[type=checkbox]').attr('checked', 'checked');
                    e.preventDefault();
                    return false;
                }
            }
        });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!