Parsear html iframe saved in a variable in JQUERY

0

I have saved the following in a variable:

register = "<iframe id='registro' src='estudios/info-estudios/'></iframe>"

Within that variable I get a table that exists in src = 'studies / info-studies /', which is the following:

<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>

What I want is to parse the field of that table with id = 'record' with JQUERY and save the result in another variable to be able to use. If I print register it shows me the whole table correctly, what I want is to be able to simply print the YES or NO value of the record field. Thank you very much.

    
asked by Esther 04.07.2018 в 14:03
source

1 answer

0

As you have it now, what is in the variable register is just a string with html code for iframe , you do not have a table, it's just text. I imagine that later you write it on the website and this is how you see the table.

With jQuery you can use .load to load the content of another page into an element of your page, but what you want is to save them in a variable. For this, what you can do is create an element dynamically, load the page in it and then read the variable.

Additionally, .load() allows you to load only specific fragments of the page by specifying an id , which It will be convenient in this case because what you want is the value that is inside the td with id "registration".

Then you could do something like this:

// crea un elemento 'div' auxiliar donde cargarás la página
var pagina = $("<div>");

// usa 'load' para cargar el id de la página especificada
pagina.load("estudios/info-estudios/ #registro", function(contenido){

   // guarda el texto del id en una variable
   var registro = pagina.text(); 
});

And remember: .load() is an asynchronous function. You should not use the value of registro outside of the callback function because it might be the case that it has not been loaded yet.

    
answered by 04.07.2018 в 14:57