EN
JavaScript - find all paragraphs on web page
0
points
In this article, we would like to show you how to find all paragraphs on web page using JavaScript.
Quick solution:
document.querySelectorAll('p');
Practical example
In this example, we use querySelectorAll() method to get the collection of all paragraphs (p elements) on the web page.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<p>Paragraph 1</p>
<script>
function findParagraphs() {
return document.querySelectorAll('p'); // finds all paragraphs
}
// Usage example:
window.addEventListener('load', function() {
console.log([...findParagraphs()]);
});
</script>
<p>Paragraph 2</p>
</body>
</html>
Note:
The
findParagraphs()function needs to be executed after windowloadevent or at the end of the script to make sure that all items are loaded before the function execution.