How to use jQuery to get the value from the table?

Hello!

There is some code:

...
 <table class="table-1">
 <tbody><tr class="row-1">
 <td><span class="name">Test</span></td>
 <td><button onclick="get_name('row-1')">Get</button></td>
 </tr><tr class="row-999">
 <td><span class="name">Test 999</span></td>
 <td><button onclick="get_name('row-999')">Get</button></td>
</tr>
</tbody></table><br>
And the function that is passed the unique identifier of the parent (in our case, row-1):<br><pre><code class="javascript">function get_name(number) {
 // it's empty
}</code></pre><br>
How to use jQuery to get the value of <code class="html"><span class="name">Test</span></code> from <code class="html"></code>? It is possible to pass the parent class is not necessary, just using this() and parent() nothing happens...<table><tbody><tr class="row-1"></tr></tbody></table>
June 3rd 19 at 18:54
2 answers
June 3rd 19 at 18:56
Solution
function get_name(number) {
 return $('.'+number).find('span.name').text();
}


is passed the unique ID of the parent

The unique identifier is id. And You have this class.
June 3rd 19 at 18:58
Solution
https://jsfiddle.net/alexbie/28odacmr/

<table class="table-1">
 <tbody><tr class="row-1">
 <td><span class="name">Test</span></td>
<td><button>Get</button></td>
</tr>
 <tr class="row-999">
 <td><span class="name">Test 999</span></td>
<td><button>Get</button></td>
</tr>
</tbody></table><br><br><pre><code class="javascript">$('.table-1').on('click', 'button', e => {
 const text = $(e.target).closest('tr').find('span.name').text();
console.log(text);
});</code></pre><table></table>
Pretty interesting solution, thank you! - Vada74 commented on June 3rd 19 at 19:01

Find more questions by tags JavaScriptjQuery