I'm trying to select the previous main tr from a nested table using JQuery but have not a solution.
There are multiple rows with the same class name but I need only the previous one.
<table >
<tr><!-- I want to go here -->
<td>hello world</td>
<td >hello world</td>
<td>hello world</td>
<td>hello world</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>som text</td>
<td></td>
</tr>
<tr>
<td>som text</td>
<td></td>
</tr>
<tr>
<td>som text</td>
<td onclick="goto(this)">some text</td><!-- I am here and I would like to go up to the previous tr outside the nested table -->
</tr>
</table>
</div>
</td>
</tr>
I have this cose but is selecting all the '.eta' classes I just need to select the previous one
function goto(t){
var eta = $('.flttbl').find('.eta')
$(eta).text(landing)
}
CodePudding user response:
You need to use closest with prev for that task like:
function goto(t) {
var eta = $(t).closest('div').closest('tr').prev().find('.eta');
$(eta).text('test')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table >
<tr>
<!-- I want to go here -->
<td>hello world</td>
<td >hello world</td>
<td>hello world</td>
<td>hello world</td>
</tr>
<tr>
<td>
<div>
<table>
<tr>
<td>som text</td>
<td></td>
</tr>
<tr>
<td>som text</td>
<td></td>
</tr>
<tr>
<td>som text</td>
<td onclick="goto(this)">some text</td>
<!-- I am here and I would like to go up to the previous tr outside the nested table -->
</tr>
</table>
</div>
</td>
</tr>
