Selection Elements

2

I have the following structure below HTML, I would like to know, as I do when I click the button, I can get value from the element of td class="description" before that within the scope of tr.

<tr>
    <td class="descricao">Texto01</td>
    <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
</tr>
<tr>
    <td class="descricao">Texto02</td>
    <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
</tr>
<tr>
    <td class="descricao">Texto02</td>
    <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
</tr>
    
asked by anonymous 29.11.2018 / 14:34

2 answers

4

You will use:

Example:

$('[type=button]').on('click', function() {
    let descricao = $(this).closest('tr').find('.descricao').text();
    console.log(descricao);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script><table><tr><tdclass="descricao">Texto01</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
    <tr>
        <td class="descricao">Texto02</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
    <tr>
        <td class="descricao">Texto03</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
</table>
    
29.11.2018 / 14:54
0

    $('button').on('click', function() {
        el = $(this).closest('tr').children('td.descricao');
        el.css('background-color', 'lightgreen');
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table><tr><tdclass="descricao">Texto01</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
    <tr>
        <td class="descricao">Texto02</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
    <tr>
        <td class="descricao">Texto03</td>
        <td class="btn-l"><button type="button">Leitura Confirmada</button></td>
    </tr>
</table>
    
29.11.2018 / 14:43