shaddles shaddles - 6 months ago 10
Javascript Question

How can I list the contents of all the <p> elements in the console using JavaScript?

I'm trying to log the content of all the

<p>
elements in the console by pressing on a button here is the code so far.

window.addEventListener("load",init);
function init(){

var b = document.getElementById("btn2")
b.addEventListener("click",action)
}
function action(){
var i, items = document.qetElementsByTagName("p");

for (i = 0; i < items.length; i++) {


console.log(items[i]);

}
}

Answer

Get content using innerHTML or textContent property

console.log(items[i].innerHTML); 
//or
console.log(items[i].textContent);
Comments