0

I am trying to wrap this function

$( "li.menu-item" ).hover(function() {  // mouse enter
    $( this ).find( " > .sub-menu" ).show(); // display immediate child

}, function(){ // mouse leave
    if ( !$(this).hasClass("current_page_item") ) {  // check if current page
        $( this ).find( ".sub-menu" ).hide(); // hide if not current page
    }
});

In:

(function($){

})(jQuery);

To eliminate the $ is not a function error in WordPress. Below is my code. I a still receiving the $is not a function error. Can someone please help me to update this code to resolve the error?

3
  • i think you're on the wrong path here. If it doesn't find $, jQuery might not be imported yet. Can you show us which template you are using or your js imports? Commented Aug 25, 2020 at 16:25
  • 1
    Have you seen this: api.jquery.com/jquery.noconflict
    – Ron
    Commented Aug 25, 2020 at 16:26
  • @GlabbichRulz jQuery is loaded fine. I've wrapped over functions using this method in the same file, I just can't seem to get this one to work.
    – mcook16
    Commented Aug 25, 2020 at 16:31

1 Answer 1

1

This normally occurs because Wordpress uses https://code.jquery.com/jquery-1.11.4.js as its default CDN. Where as lots of sites/scripts utilise more recent versions. Hence the $ is not a function error. Instead of wrapping every js function which is a massive time drain and not always practical, simply add this to your header, or as high up in the document as possible. This way you wont have to wrap all your js functions and can continue to use $

var $ = jQuery.noConflict();
2
  • Thanks, will I have to "unwrap" the rest of my functions, or can they exist as they are as well?
    – mcook16
    Commented Aug 25, 2020 at 22:57
  • They should be ok. It'll just add the noConflict to any functions currently using $
    – beatnik
    Commented Aug 26, 2020 at 15:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.