Select all elements in jQuery
In this tutorial, you will learn how to select all the elements in the document using jQuery.
Solution
To select all the elements in the document using jQuery, use the *
selector, as shown in the following.
var allElements = $("*");
Programs
1. In the following script, we have selected all the elements in the document, and printed their tag names to the console.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Select all elements in the document
var allElements = $("*");
// Do something with the selected elements
allElements.each(function() {
console.log($(this).prop("tagName"));
});
});
</script>
</head>
<body>
<h2>Hello User!</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</body>
</html>