Title: Adding Custom Javascript to Child Theme (how to format the code)
Last modified: August 21, 2016

---

# Adding Custom Javascript to Child Theme (how to format the code)

 *  Resolved [optymizer](https://wordpress.org/support/users/optymizer/)
 * (@optymizer)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-javascript-to-child-theme-how-to-format-the-code/)
 * Hello,
 * I have added a custom menu to the sidebar on one of my pages, but due to the 
   amount of pages, it is too long. [http://kinglocksmiths.com/washington-dc-locksmith/](http://kinglocksmiths.com/washington-dc-locksmith/)
 * I found this code to help me make the menu collapsible
 * [http://mysitemyway.com/docs/index.php/Collapsible_sidebar_menus](http://mysitemyway.com/docs/index.php/Collapsible_sidebar_menus)
 *     ```
       jQuery(document).ready(function(){
            jQuery('.menu .dropdown a').click(function(e){
                 e.preventDefault();
                 if (jQuery(this).parent().children('.sub-menu:first').is(':visible')) {
                      jQuery(this).parent().children('.sub-menu:first').hide();
                 } else {
                      jQuery(this).parent().children('.sub-menu:first').show();
                 }
            });
       });
       ```
   
 * But when I put this in the function.php file of the child theme, I get an error.
   I am not familiar with javascript, and don’t know if I need to change this code
   or format it differently.
 * Can you help me?
 * Thank you,
    Sarah

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [ElectricFeet](https://wordpress.org/support/users/electricfeet/)
 * (@electricfeet)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-javascript-to-child-theme-how-to-format-the-code/#post-4506292)
 * You seem to have fixed it now; your menus are collapsed.
 *  Thread Starter [optymizer](https://wordpress.org/support/users/optymizer/)
 * (@optymizer)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-javascript-to-child-theme-how-to-format-the-code/#post-4506305)
 * It took me a while (and a lot of trial and error), but I found this plugin:
 * [Custom JavaScript Editor](http://wordpress.org/plugins/custom-javascript-editor/)
 * And after creating the menus and placing the CSS as stated [here](http://mysitemyway.com/docs/index.php/Collapsible_sidebar_menus)
   I placed inside the Custom JavaScript Editor this code:
 *     ```
       /*global document */
       jQuery(document).ready(function () {
           "use strict";
           jQuery('.menu .dropdown a').click(function (e) {
               e.preventDefault();
               if (jQuery(this).parent().children('.sub-menu:first').is(':visible')) {
                   jQuery(this).parent().children('.sub-menu:first').hide();
               } else {
                   jQuery(this).parent().children('.sub-menu:first').show();
               }
           });
       });
       ```
   
 * and checked the box to “Load also: JQuery” on the right.
 * It makes the menu item dropdown when you click the link (instead of going to 
   the linked page) [http://kinglocksmiths.com/washington-dc-locksmith/](http://kinglocksmiths.com/washington-dc-locksmith/)
 * Hope this helps someone… 🙂

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Adding Custom Javascript to Child Theme (how to format the code)’ is 
closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/customizr/4.4.24/screenshot.png)
 * Customizr
 * [Support Threads](https://wordpress.org/support/theme/customizr/)
 * [Active Topics](https://wordpress.org/support/theme/customizr/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/customizr/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/customizr/reviews/)

## Tags

 * [child theme](https://wordpress.org/support/topic-tag/child-theme/)
 * [javascript](https://wordpress.org/support/topic-tag/javascript/)

 * 2 replies
 * 2 participants
 * Last reply from: [optymizer](https://wordpress.org/support/users/optymizer/)
 * Last activity: [12 years, 4 months ago](https://wordpress.org/support/topic/adding-custom-javascript-to-child-theme-how-to-format-the-code/#post-4506305)
 * Status: resolved