How to get string data of inputs in DataTables rows

DataTables

Sometimes we have some problems to get data from a row in a table specially whe the data is an input tag likes checkbox, radio or text input. With fnGetData() we obtain an array of td’s of the row but in string format.

If we want to obtain all data of some cell of the row in an html format, we have to do this:

Javascript:

$(document).ready(function() {
  //datatable creation
  var oTable = $('#example').dataTable();

  //foreach rows of the table
  $(oTable.fnGetNodes()).each(function(index,elem)){
    var aPos  = oTable.fnGetPosition(this);
    var aData = oTable.fnGetData(aPos[0]);
    
    //we get array of strings
    var tdArray = aData[index];

    //now is the moment to convert our tr and td's tags into html tags
    var nRow = $('#example tbody tr:eq('+index+') td').parent('tr')[0];

    //and if we want to get only all the input tags
    var jqInputs = $('input', nRow);
    
    //and to go directly to an specific input of the row. For example in the cell number 1 this is a input
    var input = jqInputs[1];
    console.log(input);

    //and we have to get all attributes of the input
    console(input.name);
    console(input.value);

    //or using jQuery
    console.log('#' + input.id).val());
    console.log('#' + input.id).attr('yourAttribute');
    console.log('input[name["' + input.name + '"]').attr('yourAttribute');
    //know if input is cheched or not
    console.log('input[name["' + input.name + '"]').is(':checked');
  });
});

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s