Eli Eli - 25 days ago 9
CSS Question

How to get the value of an input field inside the table td using javascript selector?

I have this line of code where it gets the value inside the td part of the table.

document.getElementById("supplierID").value = $tr.find('td:eq(0)').html();


The above code successfully get the data inside the td part of the table. E.g

<td>{{ $val->fname }}</td>


However, if the td has input field inside it

<td><input type='checkbox'style='width:30px; height:20px;' class='radio_check_all prod-id-checkbox' id='prod-id-checkbox' value="{{ $val->id }}"></td>


it returns the markup code of an input field using the code shown above. It doesn't get the right value. How would I do it? Please help. Thanks.

Answer

The following code snippet should work

check this

$(function(){
  var table=$("table");
  alert(table.find('tr td:eq(0) input').val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td><input type='checkbox' style='width:30px; height:20px;' class='radio_check_all prod-id-checkbox' id='prod-id-checkbox' value="{{ $val->id }}"></td>
  </tr>
</table>

Hope this helps