jQuery – Universal * selector example

The universal * selector is used to select all elements, everything.

Examples

  1. $(‘*’): selects all elements in the document.
  2. $(‘div > *’): selects all elements that are children of a <div> element.

In general, using the universal alone didn’t make sense, at least i can’t think of any use cases. But, it’s always used to combined with other elements to form a new custom selector expression.

jQuery Example

A simple example to show the use of the jQuery universal * selector.


<html>
<head>
<title>jQuery universal example</title>
 
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

<style type="text/css">
	div{
		padding:8px;
	}
</style>
</head>

<body>

<h1>jQuery universal example</h1>

<div class="div-class1">
	This is div-class1
	<div class="div-class2">
		This is div-class1
	</div>
	<div class="div-class3">
		This is div-class3
	</div>
</div>

<br/><br/>
<button>*</button>
<button>div > *</button>
<button id="refresh">Refresh</button>

<script type="text/javascript">
    $("button").click(function () {
       var str = $(this).text();
       $(str).css("border", "1px solid #ff0000");
    });
	
    $('#refresh').click(function() {
    	location.reload();
    });

</script>

</body>
</html>

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
jQuery Tutorials Recent comment authors
newest oldest most voted
trackback
jQuery Tutorials

[…] Universal * selector Select all elements, everything. […]