user13023812
user13023812

Reputation:

Find td in a table from another td child

Basically i want to find the value of the td element of the class "number" value when i press the input button.

<table id="tableid">
 <tbody>
         <tr>
            <td> </td>
            <td class="number"> 5 </td>
            <td> </td>
            <td> </td>
            <td><input type="button" onclick="value(this)"></td>
        </tr>
 </tbody>
</table

This is the javascript which is not working:

function value(row) {
    var number = row.parentNode.parentNode.find("td:eq(1)").text(); 
    alert(number)
 }

What i'm missing?

Upvotes: 1

Views: 607

Answers (2)

Stoyan Bukovich
Stoyan Bukovich

Reputation: 87

Hello I assume this is general question and you are not asking for finding dynamically created table. I suggest you to go classic JAVASCRIPT

function myFunction() {
  var x = document.getElementsByClassName("number")[0].innerHTML;
  alert(x);
}

Cheers

Upvotes: 0

Taplar
Taplar

Reputation: 24965

$(row).closest('tr').find('.number').text();

Use closest to go up to the row, then find the element by its class, and then get its value.

Upvotes: 2

Related Questions