Title: jQuery code not working with Code Snippet
Last modified: May 1, 2022

---

# jQuery code not working with Code Snippet

 *  Resolved [sarangsss29](https://wordpress.org/support/users/sarangsss29/)
 * (@sarangsss29)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/jquery-code-not-working-with-code-snippet/)
 * How can I use the following code using Code snippet?
    I can use the jQuery code
   with other plugins like **Simple Custom CSS and JS** However, it throws error
   when I try to use it via Code Snippet. I don’t want to use a new plugin because
   I already have several code snippets implemented.
 * How can I add this code?
 *     ```
       jQuery(document).ready(function($) {
           /*Click on dark mode icon. Add dark mode classes and wrappers. 
           Store user preference through sessions*/
           $('.wpnm-button').click(function() {
               //Show either moon or sun
               $('.wpnm-button').toggleClass('active');
               //If dark mode is selected
               if ($('.wpnm-button').hasClass('active')) {
                   //Add dark mode class to the body
                   $('body').addClass('dark-mode');
                   //Save user preference to Storage
                   localStorage.setItem('darkMode', true);
               } else {
                   $('body').removeClass('dark-mode');
                   localStorage.removeItem('darkMode');
               }
           })
           //Check Storage. Display user preference 
           if (localStorage.getItem("darkMode")) {
               $('body').addClass('dark-mode');
               $('.wpnm-button').addClass('active');
           }
       })
       ```
   

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/jquery-code-not-working-with-code-snippet/#post-15621278)
 * Take a look at this article for how to add CSS or JS code to your site’s head:
   [https://help.codesnippets.pro/article/41-how-can-i-add-code-to-my-site-s-header-or-footer](https://help.codesnippets.pro/article/41-how-can-i-add-code-to-my-site-s-header-or-footer)
 * Make sure to also wrap it in `<script>` `</script>` tags.

Viewing 1 replies (of 1 total)

The topic ‘jQuery code not working with Code Snippet’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/jquery-code-not-working-with-code-snippet/#post-15621278)
 * Status: resolved