Display the contents of Object Nodelist

Asked

Viewed 996 times

4

How to display received content through a objectNodeList ?

Through the following function I try to get the content of tags by the class.

function final(){
    var titulos = document.querySelectorAll(".p2.p2-resultado-busca");
    var result = document.getElementById('result');
    result.innerHTML = titulos;
}

However it returns an object, how to display this content ?

  • that this izzas

2 answers

3


The estate innerHTMLexpecting a String. That’s why it’s not working.

You can go through titulos and use appendChild to insert each element found in your desired element:

function final(){
    var titulos = document.querySelectorAll(".p2.p2-resultado-busca");
    var result = document.getElementById('result');
    //result.innerHTML = titulos;


     [].forEach.call(titulos, function(el) {
         result.appendChild(el);
     });

}

Observing: I used Array().forEach.call in that call due to the object NodeList do not possess this method.

  • show man vlw erea isso ai

2

Can make a simple loop to get the content:

var titulos = document.querySelectorAll(".p2.p2-resultado-busca");

var content = '';
for(var i = 0; i < titulos.length; i++)
    content += titulos[i].textContent;

document.getElementById('result').textContent = content;
<p class='p2 p2-resultado-busca'>a</p>
<p class='p2 p2-resultado-busca'>b</p>
<p class='p2 p2-resultado-busca'>c</p>

<p id='result'></p>

  • mto good man vlw

Browser other questions tagged

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