Yohan Blake Yohan Blake - 5 months ago 8x
PHP Question

Get value of td of table using javascript

I want to get the value of a

. I am populating the table from a database, and I can't get the value of a specific field using it's name because the rows are looped and if I use the name, I get the value of that field from the first row. So for that, I wrote my js like this:

function searchcatg(el2){
var catel = $(el2);
var searchTxt = catel.val();
//var brandval = $("input[name='brandid']").val();
var brandval = $(el2).closest("tr").find('td:eq(7)').text();
$.post("search-cat-brand-vw.php", {searchVal: searchTxt, brand:brandval})
.done(function(catglist) {



I get the first value correctly, but the value
doesn't properly show up. On the alert box it shows some unreadable characters and in the php I post to, it gets a blank. This is how the
field looks like

<td><input type="text" name="brandid" value="<?php echo $brandid; ?>" list="brandlist" id="output" onkeyup="searchbrand(this)" >
<datalist id="brandlist" name="taskoptionbrand">
<option> </option>
</datalist> </td>

in the
var brandval
says that it's undefined. What do I do?


The TD never contain any value, so you can't use the val() or value in it. You need to find the input box into the TD and then use the val().

As i comment it out, you need to change in your function searchcatg. Search for the replacement.




$(el2).closest("tr").find('td:eq(7) input').val();