Universal Selector *

In this post of JQuery, I will introduce you to universal selector, represented by * symbol.

It is used to select all the elements in the html page.

Below is an example


1  <html>
2   <head>
3       https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
4   </head>
5   <body>
6       
7           function selectAll() {
8               var $all = $('*');
9               alert($all);
10          }
11      
12      <input name="button" type="button" value="button" onclick="selectAll();"/>
13  </body>
14 </html>

In the above html code, on click of button I am calling javascript selectAll method.

In the javascript method, I am selecting all the elements from beginning of head tag to end of it, using the below code


$('*')

Or

jQuery('*')

jQuery or its alias ‘$’ symbol is a function that accepts two arguments. The first argument is ‘selector’ and second one is a ‘context’ (optional).

‘selector’ defines a criteria which is used to identifies the elements to be returned and ‘context’ gives selector the scope of the document within which
the selector has to select the elements meeting the criteria.

The function returns an object which is collection of DOM elements matching the criteria.

The selector in the above example is ‘*’ which selects all the elements within the context and in this case absence of context indicates the whole document as the context.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s