Recover DOM element from a column

Asked

Viewed 30 times

1

I have an event in Jquery that flags information from a table row (I’m using Jquery Datatables).

var linha = $(this).parents('tr')[0].innerHTML;

The value returned is this:

<td class="text-center sorting_1">118587</td>                                   
<td class="font-w600">113326</td>                                 
<td class="d-none d-sm-table-cell">JOÃO DA SIILVA(<b>68</b>)</td>                                   
<td class="text-center">
    <button id="btnIncluiVisitante" class="btn btn-primary">
        <i class="fa fa-user-plus"></i><label for=""></label>                                       
    </button>
    <button id="btnRemoveVisitante" class="btn btn-primary">                                            
        <i class="fa fa-user-times"></i>
        <label id="qtdeVisitantes">1</label>
    </button>                                     
</td>

How do I get the value 1 that’s on the tag <i> with id="qtdeVisitantes"?

  • 1

    A tip: Avoid mixing Vanilla syntax with jQuery.

1 answer

2


If you want to take from the first object it would be:

$(linha).find("#qtdVisitantes").text();

If #qtdVisitants is unique (as it should be) just this code:

$("#qtdeVisitantes").text();

Another syntax to pick up the value (I imagine that at the click of the btnRemoveVisitant button) would be:

$(this).closest("tr").find("#qtdVisitantes").text();

NOTE: I imagine that there are many elements with the same id #qtdVisitants, in this case it is not suitable, because ID’s should be unique, if it is possible to change by class.

  • 1

    Thank you John, that’s right. The fact that it has several id #qtdVisitants had previously dealt with. My error is in not using the "line" variable as a Jquery variable. I have now put the $(line) variable and I can use the find method().

  • 1

    Yes, when we put into the jquery $() selector a string containing html it automatically transforms into a jquery object, allowing you to use all functions of it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.