custom drop down menu javascript issues in wordpress

I am learning WordPress and have a query with some custom jQuery javascript I use for a drop down menu.

The javascript is used to put a slight delay on the drop down hiding after mouseoff of either the parent link or the drop down etc.

The javascript works great in normal html but not in the basic WordPress theme I am building. I tried following the no-conflict $j advice but not sure if I did that right.

One issue is that the drop down menu stays persistent on mouseover of the parent link – it does not hide after the 2 seconds specified in the javascript.

Another issue, which may be related, appears to be that the “scroll compensate” code is not working because the “sub menu” drop items show on hover of the parent link when smaller than 650px if the drop down menu was not showing (eg on page refresh without interacting with menu).

I think it may be related to the line “var menu_timer;” at the top of the custom.js file but it could be that the code is not working above or below the line:


The custom.js javascript file is enqueued in functions.php and showing in the footer as expected. All other javascripts in the custom.js file are working, just not the drop down menu code which is shown below in the order (eg above and below the doc ready line) that the programmer gave it to me

// menu
var menu_timer;

//jquery doc ready
jQuery(document).ready(function($) {

    //recalculate when resizing 
    $(window).resize(function() { 


// no conflict
var $j = jQuery.noConflict();

// menu
 function submenuPersistent() {
    //depends on screen size
    if($j(window).width()+scrollCompensate() >= 651) {
        $j('#nav > ul > li .drop').hide();
        //persistent submenu
        $j('#nav > ul > li').off('mouseenter');
        $j('#nav > ul > li').off('mouseleave');
        $j('#nav > ul > li').mouseenter(function() {
            //hide other submenus
            if($j(this).find('.drop').length != 0){//has submenu
                clearTimeout(menu_timer);//clear timeout
                $j('#nav > ul > li').not(this).find('.drop').hide(200);//hide others
                $j(this).find('.drop').show(200);//show this

        $('#nav > ul > li').mouseleave(function() { 
            if($j(this).find('.drop').length != 0){//has submenu
                element= $(this);
                menu_timer= setTimeout(function() {
    else {
        //clear timeout
        $j('#nav > ul > li').off('mouseenter');
        $j('#nav > ul > li').off('mouseleave');
        $j('#nav > ul > li .drop').show();

 //compensate for Chrome/Safari bug (scroll bar width issue)
 function scrollCompensate() {
    var inner = document.createElement('p'); = "100%"; = "200px";
    var outer = document.createElement('div'); = "absolute"; = "0px"; = "0px"; = "hidden"; = "200px"; = "150px"; = "hidden";
    var w1 = inner.offsetWidth; = 'scroll';
    var w2 = inner.offsetWidth;
    if (w1 == w2) w2 = outer.clientWidth;
    return (w1 - w2);

The site is still very much in development so I cannot show a live page at the moment unfortunately.

I would greatly appreciate any help from an experienced javascript / WordPress user.

Any ideas?

Read more here: custom drop down menu javascript issues in wordpress

Leave a Reply

Your email address will not be published. Required fields are marked *