Warning: session_start(): open(/tmp/sess_09b81aa970189b95b6e6d7b5b559878f, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to solve TypeError: addClass is not a function in JavaScript - LearnShareIT

How to solve TypeError: addClass is not a function in JavaScript

TypeError: addClass is not a function in JavaScript

TypeError: addClass is not a function in JavaScript, a common error you often encounter when working with jQuery functions. Our explanations below will help you solve this kind of error with causes and solutions.

Why does the TypeError: addClass is not a function in JavaScript happen?

addClass() is a method that is included in many jQuery libraries but not the JavaScript built-in method. This method helps you add the specified class to the elements which have called the method. The error “addClass is not a function” is raised, implying that this method addClass() is not found in the library that you have imported; it might be due to you calling the function on a value that is not a set of jQuery elements. For instance:

document.body.addClass('learnshareit');

Result:

Another example:

$('html').addClass()

Result:

Furthermore, it may occur when you have imported any jQuery library in the script tag. For example:

<html>
  <body>
    <h1>LearnShareIT</h1>
    <script type="text/javascript">
        let learnShareIT = "learnshareit"
        learnShareIT.addClass('learnshareit')
    </script>
  </body>
</html>

Output:

To solve the error, we recommend two following solutions.

How to solve this error?

Check the type of the object calling the addClass()

You should remember that this function addClass() only works if you are calling it from a set of jQuery elements. Therefore, you must make sure it is not an HTML element but a jQuery element, for example:

$('head').addClass('LearnShareIT')
console.log(document.head)

Output:

<head class="LearnShareIT"></head>

Also, make sure you have checked the spelling of the word ‘addClass’ because it is case-sensitive, and misspelling it will lead to an error.

Check if you have declared the jQuery library

As we have explained, this error will happen if you don’t import the library jQuery into the HTML page. To handle this problem, make sure you have placed the JavaScript <script> tag above the HTML code that defines the document’s elements. We suggest you put the tag at the end of your body tag (before closing the body tag) so it can work only if the dom elements have been loaded:

<html>
  <body>
    <h1>LearnShareIT</h1>
    
    <script src  ="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
        $('head').addClass('LearnShareIT')
        console.log(document.head)
    </script>
  </body>
</html>

Output:

<head class="LearnShareIT"></head>

As can be seen, we import the jQuery-min version as in the example above. Although there are many versions that contain this addClass function, we still recommend you use the latest version because it can help you avoid some other errors during use. Therefore, if you make changes to the version you are using, the results may not be what you expect. In many cases, loading jQuery multiple times or multiple versions on the same page can lead to this error, so please declare it once on each page to prevent the error.

Summary

We have discovered many approaches to tackle the TypeError: addClass is not a function in JavaScript. We suggest you check the type of jQuery elements and make sure to import only one version of jQuery to your page, and you can also check your spelling or use another jQuery version as introduced here.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *