Ashe Ashe - 1 month ago 6
HTML Question

How get the value in <tr>

I need to get the text in

<tr>
. This is my html code:

<table class="table" style="" id="tabella_username_aggiunti">
<thead>
<tr>
<th>Username</th>
<th>Cancella</th>
</tr>
</thead>
<tbody id="tbody_aggiungi_utente"><tr id="aggiungiprova"><td>prova</td><td><button id="eliminaprova" type="button" class="btn btn-danger btn-sm"><i class="fa fa-times" aria-hidden="true"></i></button></td></tr></tbody>
</table>


I need to extract the first
<td>
in
<tr>
that is "prova" in this example so I use this jquery code:

$('#tbody_aggiungi_utente').children('tr').each(function(i) {
console.log("TABELLA " + JSON.stringify(i)+" Tabella "+JSON.stringify($(this)));
//array_user.push($(this).attr('id').replace('aggiungi', ''));

});


But it doesn't work because it prints me
TABELLA 0 Tabella {"0":{},"context":{},"length":1}


Anyone can help me?

Answer

$('#tbody_aggiungi_utente').children('tr').each(function(i) {
 console.log("TABELLA " + JSON.stringify(i)+" Tabella "+$(this).first().text());
                                            //array_user.push($(this).attr('id').replace('aggiungi', ''));

                                        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" style="" id="tabella_username_aggiunti">
        <thead>
            <tr>
              <th>Username</th>
              <th>Cancella</th>
            </tr>
        </thead>
        <tbody id="tbody_aggiungi_utente">
          <tr id="aggiungiprova"><td>prova</td><td><button id="eliminaprova" type="button" class="btn btn-danger btn-sm"><i class="fa fa-times" aria-hidden="true"></i></button></td></tr>
  </tbody>
      </table>

Comments