Check if object is a jQuery object

Given an object, we have to check whether it is a jQuery object or not.
Submitted by Pratishtha Saxena, on September 17, 2022

Prerequisite: Adding jQuery to Your Web Pages

An object, in jQuery, can be understood as an array-like structure that can have content inside. This object does have a length property, index, selector, etc. The content specifically inside this can be in the format – property1: value1; property2: value2; .. and so on.

When creating an empty object, declare {}. This will initialize the object. The property:value can be declared inside this curly brackets. Another way of creating an object is by using a selector. For this use $('selector').

Since now we have a basic knowledge of what an object is and how it is declared, let's move on to how to check whether the created object is a jQuery object or not.

The following are the two ways to do this.

Check if object is a jQuery object using type() method

The type() method in jQuery helps to know the data type or the structure of the variable specified. If the variable is a string, Boolean, number, etc., this will return it.

Syntax:

jQuery.type(abc);

Hence, if the specified object is a jQuery object, then it will return an object as a result.

Example to Check if object is a jQuery object using type() method

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <title>Document</title>
   </head>
   
   <body>
      <h1 id="one">Check if Object is jQuery Object</h1>
   </body>
   
   <script type="text/javascript">
      $(document).ready(function(){
          var abc = {Name: 'Tom', Age: '20', Gender: 'Male'};
          var name = $('#one');
          // Method 1
          console.log(jQuery.type(abc));
      });      
   </script>
</html>

Output:

Example 1: Check if object is a jQuery object

Check if object is a jQuery object using instanceof operator

This operator helps to check the type of the specified object at the run time. This operator returns 'true' if the object belongs to the instance of a particular class and 'false' if not. Therefore, if the object is a jQuery object it should return 'true' otherwise 'false' as its result.

Syntax:

objectName instanceof objectType

Here, we'll specify the objectType as jQuery. Hence, below is an example for a better understanding of this.

Example to Check if object is a jQuery object using instanceof operator

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <title>Document</title>
   </head>
   
   <body>
      <h1 id="one">Check if Object is jQuery Object</h1>
   </body>
   
   <script type="text/javascript">
      $(document).ready(function(){
          var abc = {Name: 'Tom', Age: '20', Gender: 'Male'};
          var name = $('#one');
      
          //Method 2
          if (name instanceof jQuery){
              console.log('This is a jQuery Object.')
          }
      });
   </script>
</html>

Output:

Example 2: Check if object is a jQuery object





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.