How can I get a cell value when I select a table row?

How can I get a cell value when I select a table row?

gbeardgbeard Posts: 2Questions: 1Answers: 0

I have a table setup, I need to get the value of the first cell and store it into a var. I can click on the cell and it works, I need to be able to click on the row and get the first cell value.

$(document).ready(function() {
var table = $('#tmcustlist').DataTable();

            $('#tmcustlist tbody').on( 'click', 'td', function () {
            if ( $(this).hasClass('selected') ) {
              $(this).removeClass('selected');
           }
            else {
                table.$('tr.selected').removeClass('selected');
                $(this).addClass('selected');
            }



            var custid = (table.cell(this).data()); // This is where I need the data stored.

            var link;
            link = 'marketreg.php?task=tmMarketReg&id=' + custid;
            window.location = link;

        } ); 

    } );

Answers

  • mrd05dmrd05d Posts: 45Questions: 6Answers: 3
    $("#example").on("click", "tr", function() {           
        console.log($(this).children(":first").text());
    });
    

    so to store in a var would simply be:

    $("#example").on("click", "tr", function() {           
        var $name = $(this).children(":first").text();
    });
    
  • gbeardgbeard Posts: 2Questions: 1Answers: 0

    This is how I fixed my issue, I took the result from my label row and did a string manipulation to get the first characters before the ',':

    $(document).ready(function() {
                     var table = $('#tmcustlist').DataTable();
                     
                    
                    $('#tmcustlist tbody').on( 'click', 'tr', function () {
                    if ( $(this).hasClass('selected') ) {
                      $(this).removeClass('selected');
                   }
                    else {
                        table.$('tr.selected').removeClass('selected');
                        $(this).addClass('selected');
                    }       
                    var custid = (table.row(this).data());              
                    var custString = custid.toString();
                    var custidFix = custString.substr(0, custString.indexOf(','));
                    
                    
                    
                    var link;
                    link = 'marketreg.php?task=tm&id=' + custidFix;
                    window.location = link;
                    
                } ); 
    
    
This discussion has been closed.