shaddles
shaddles

Reputation: 141

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]);

    }
}

Upvotes: 0

Views: 199

Answers (2)

codemirror
codemirror

Reputation: 3582

(Plain JavaScript) To list all "p" use

console.log(document.getElementsByTagName("p"))

Or use for first "p" only

//console.log(document.getElementsByTagName("p")[0])

To get content of first "p"

console.log(document.getElementsByTagName("p")[0].textContent)

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115242

Get content using innerHTML or textContent property

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

Upvotes: 2

Related Questions