Parsear html with JQUERY

0

From the table I show below, could someone tell me how to save the value of the table belonging to 'REGISTRATION' in JQUERY in a variable? That is to say that depending on the value of REGISTRATION, which can be YES or NO, keep that in a varibale to use later.

<table id="info-table" class="table table-bordered">
<tbody>

    <tr class="active">
        <td>VALOR</td>
        <td>
            152
        </td>
    </tr>

    <tr class="active">
        <td>REGISTRO</td>
        <td>
            No
        </td>
    </tr>

</tbody>

Thank you very much.

    
asked by Esther 04.07.2018 в 13:10
source

2 answers

0

In case you only need the value once, the easiest thing is to give an id to the td and then pick it up with Jquery.

   <table id="info-table" class="table table-bordered">
    <tbody>

        <tr class="active">
            <td>VALOR</td>
            <td>
                152
            </td>
        </tr>

        <tr class="active">
            <td>REGISTRO</td>
            <td id="registro">
                No
            </td>
        </tr>

    </tbody>


var registro = $('#registro').text();

In case of doing it without id you would have to look for the tr that you need and take the text of the td.

$("td:contains('REGISTRO')").next().text();

or

$("td:contains('REGISTRO')").parent().children('td:nth-child(2)');
    
answered by 04.07.2018 в 13:23
0

If your table is fixed, as you say, then you can use its id and look directly for the value of the second td of the last row with something like: $('#info-table tr:last td:eq(1)').text();

The advantage that would have to do so is that you avoid having to travel the entire table in search of a value.

Note that I have also applied trim , to clean the remaining spaces in the value that will be recovered in td .

Here is a test code:

var tdDato = $('#info-table tr:last td:eq(1)').text().trim();
console.log(tdDato);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">

<table id="info-table" class="table table-bordered pure-table">
  <tbody>

    <tr class="active">
      <td>VALOR</td>
      <td>
        152
      </td>
    </tr>

    <tr class="active">
      <td>REGISTRO</td>
      <td>
        No
      </td>
    </tr>

  </tbody>
</table>

I hope it's useful.

    
answered by 04.07.2018 в 15:28