0

My JS file is being properly called in my functions.php file because there is no error in the console when I inspect element. Why is this js code not running? Do I need to wrap the function? Everything I tired did not work. I am no js expert, but I think this code should work... It worked in my codepen.

Note: I am calling the script in the footer. Should I be calling it in my header since it is for my mobile header menu?

// Mobile Menu

$('.hamburger').on('click', function () {

    $('.main-navigation').toggleClass('open');

});
8
  • 1
    Do you have "hamburger" as the class on the button? Do you have "main-navigation" as the class on the menu element? Commented Aug 9, 2016 at 20:21
  • yeah :P let me double check spelling Commented Aug 9, 2016 at 20:26
  • I am getting an error now Uncaught type error: $ is not a function Commented Aug 9, 2016 at 20:27
  • Then you are missing jQuery or it is not loaded before your script loads. Commented Aug 9, 2016 at 20:30
  • 1
    If that were the case, you wouldn't be getting that error -- unless you are loading it and called noConflict() on it, in which case you can't use $ and have to use jQuery instead wherever you have a dollar sign. api.jquery.com/jquery.noconflict Commented Aug 9, 2016 at 20:32

1 Answer 1

1

It should work fine via the footer. You could try wrapping it in a

$( document ).ready(function() {
   $('.hamburger').on('click', function () {
      $('.main-navigation').toggleClass('open');
   });
});

How are you calling the jQuery? Are you placing it in a shortcode to a function in your functions.php, or directly in a tag inside the footer?

Last question (sorry, i'm new - and thorough): Have you checked your console? Any other Java errors?

  • sorry this is a mess, I am trying to figure out the formatting for answers/comments
Sign up to request clarification or add additional context in comments.

6 Comments

no other errors. I changed the $ to jQuery and now I am getting no errors in console, but the menu is still not working. Let me look into the css
I am calling it in functions.php and placing in footer
View your source - is the code actually being outputted to the footer when you load the page? Sometimes i've had to use ob_start(); and return with functions in Wordpress rather than ?> or echo statements.
Not sure of my versions of jquery off the top of my head...try: $( ".hamburger" ).click(function(e) { e.preventDefault(); $('.main-navigation').toggleClass('open'); }); If it works i'll edit my original answer.
Otherwise you could use an each statement: $('.hamburger').on('click', function () { $('.main-navigation').each(function(){ $(this).toggleClass('open'); }); });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.