Getting Javascript to kick in
-
Hi There,
Using Twenty Thirteen child theme – adding a drop-down navigation menu which uses javascript.
I’ve got everything in place and I’ve read the codex and followed it to the letter but still no movement.
I’ve put the js file in my child theme folder – added a link to the header.php – can’t work out where I’m going wrong. Any help would be amazing!
Thank you
-
Are you able to post a link to the site?
Hi Jose,
It’s at http://www.inverrestaurant.co.uk/test
The javascript I’m adding is from http://tympanus.net/codrops/2012/10/04/custom-drop-down-list-styling/ – example 3.
Where is the code residing and how are you loading it?
The code is in a document called menu.js in the themes folder. I’m calling it in the header.php with this html……
<script type=”text/javascript” src=”<?php bloginfo(‘template_url’); ?>/inver/menu.js”></script>
Ah, makes a little more sense now!
What you actually want to do is have the file in the child theme folder rather than the parent theme. That way if the theme gets updated you won’t lose that file. š
From there rather than hardcoding the
script, you enqueue it. That way you can declare any dependencies if there are any.In the
functions.phpfile you would add something like:add_action( 'wp_enqueue_scripts', 'my_child_scripts' ); function my_child_scripts(){ wp_enqueue_script( 'menu-js', get_stylesheet_directory_uri() . '/menu.js', array( 'jquery' ), '', true ); }Or something along those lines. A little more information about enqueuing styles and scripts can be found on the codex as well: https://codex.ww.wp.xz.cn/Function_Reference/wp_enqueue_script
ps. I took a quick look and the JavaScript file appears to be empty. It loaded fine.
Hi Jose,
Thanks so much for taking a look at this..I really appreciate you help.
I just checked the menu.js file on the server and there is code in there, I’m just wondering if the file is headed up correctly – I’ve copy and pasted exactly what is there…..
[ Moderator note: Code fixed, please wrap code in backticks or use the code button. ]
//... obj.dd.on('click', function(event){ $(this).toggleClass('active'); return false; }); //... $(function() { var dd = new DropDown( $('#dd') ); $(document).click(function() { // all dropdowns $('.wrapper-dropdown-1').removeClass('active'); }); }); function DropDown(el) { this.dd = el; this.opts = this.dd.find('ul.dropdown > li'); this.val = []; this.index = []; this.initEvents(); } DropDown.prototype = { initEvents : function() { var obj = this; obj.dd.on('click', function(event){ $(this).toggleClass('active'); event.stopPropagation(); }); obj.opts.children('label').on('click',function(event){ var opt = $(this).parent(), chbox = opt.children('input'), val = chbox.val(), idx = opt.index(); ($.inArray(val, obj.val) !== -1) ? obj.val.splice( $.inArray(val, obj.val), 1 ) : obj.val.push( val ); ($.inArray(idx, obj.index) !== -1) ? obj.index.splice( $.inArray(idx, obj.index), 1 ) : obj.index.push( idx ); }); }, getValue : function() { return this.val; }, getIndex : function() { return this.index; } }..I’ve put the menu.js in the child theme folder š and added this to the functions.php
[ Moderator note: Code fixed, please wrap code in backticks or use the code button. ]
add_action( 'wp_enqueue_scripts', 'my_child_scripts' ); function my_child_scripts(){ wp_enqueue_script( 'menu-js', get_stylesheet_directory_uri() . '/menu.js', array( 'jquery' ), '', true ); }Thanks a lot Mod š Sorry newbie here x
Thanks a lot Mod š Sorry newbie here x
It happens. š
Great! Took a second look and the content is there, being loaded from the child theme! There are a few errors, that are being thrown if you open your developer tools console.
Both of the ones that matter are the JavaScript related ones. Both of which are
Uncaught Referenceerrors. What this means is that two things are not defined in your scripts. The first one isnews()and the second one isobj. Unfortunately these forums are not meant for JavaScript issues but I hope that does give you some information and steer you in the right direction a little better. šI don’t really know what it means but now I know where to look I might have a chance of solving it.
Thanks very much Jose! You’ve been great. š
The topic ‘Getting Javascript to kick in’ is closed to new replies.