Ioan S.
Ioan S.

Reputation: 164

How to get the Textfield value from cell in DataTables?

I'd like to get a textfield value from a cell in datatables. I started considering it can be received as a simple cell but it seems it's not.

$("#orderTable tbody").on('click','.confirmBt',function(){
    let row = $(this).closest('tr');
    console.log(row.find('td:eq(7)').text());
});

In this code I just want to print the value of the texfield, so I tried with .text() but nothing's printed. Any help or idea would be welcome,

thanks for help :)

Upvotes: 0

Views: 26

Answers (1)

Hien Nguyen
Hien Nguyen

Reputation: 18965

You need change from

console.log(row.find('td:eq(7)').text());

to

console.log(row.find('td').eq(7).text());

Updated

If you want to get cell value with input tag

 let cell = row.find('td').eq(7).find('input');

 console.log($(cell).val());

$("#orderTable tbody").on('click','.confirmBt',function(){
    let row = $(this).closest('tr');
    
    let cell = row.find('td').eq(1).find('input');
    //console.log($(cell))
    console.log($(cell).val());
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

tr:nth-child(even) {
  background-color: #dddddd;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<table id="orderTable">
  <tr>
    <th>Company</th>
    <th>Contact</th>
    <th>Confirm</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td><input type='text'></td>
    <td><button class='confirmBt'>Confirm</button</td>
  </tr>
  <tr>
    <td>Centro comercial Moctezuma</td>
    <td>Francisco Chang</td>
     <td><button class='confirmBt'>Confirm</button</td>
  </tr>
 
 
</table>

</body>
</html>

Upvotes: 1

Related Questions