View the contents of object NodeList

4

How to display content received through objectNodeList ?

Through the following function I try to get the contents of the tags through 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 do you display it?

    
asked by anonymous 28.11.2016 / 14:05

2 answers

3

The innerHTML property expects String . So 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);
     });

}

Note : I used Array().forEach.call in this call because the object NodeList does not have this method.

    
28.11.2016 / 14:08
2

You 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>
    
28.11.2016 / 14:15