• I want to integrate custom jquery for my navigation bar and it fails to look? Kindly help me look into it and let me know what i am getting wrong? In my console, i got the below error message

    Uncaught ReferenceError: jQuery is not defined
    <anonymous> http://localhost/earnds.com/wp-content/themes/earn-ds/assets/js/navigation.js?ver=1.0.0:50

    Also, here is my full custom jquery

    /**
    * File navigation.js.
    *
    * Handles toggling the navigation menu for small screens and enables TAB key
    * navigation support for dropdown menus.
    */

    (function($) {
    // search-box open close js code
    let navbar = document.querySelector(".navbar");
    let searchBox = document.querySelector(".search-box .bx-search");
    // let searchBoxCancel = document.querySelector(".search-box .bx-x");

    searchBox.addEventListener("click", ()=>{
    navbar.classList.toggle("showInput");
    if(navbar.classList.contains("showInput")){
    searchBox.classList.replace("bx-search" ,"bx-x");
    }else {
    searchBox.classList.replace("bx-x" ,"bx-search");
    }
    });

    // sidebar open close js code
    let navLinks = document.querySelector(".nav-links");
    let menuOpenBtn = document.querySelector(".navbar .bx-menu");
    let menuCloseBtn = document.querySelector(".nav-links .bx-x");
    menuOpenBtn.onclick = function() {
    navLinks.style.left = "0";
    }
    menuCloseBtn.onclick = function() {
    navLinks.style.left = "-100%";
    }


    // sidebar submenu open close js code
    let htmlcssArrow = document.querySelector(".htmlcss-arrow");
    htmlcssArrow.onclick = function() {
    navLinks.classList.toggle("show1");
    }
    let moreArrow = document.querySelector(".more-arrow");
    moreArrow.onclick = function() {
    navLinks.classList.toggle("show2");
    }
    let jsArrow = document.querySelector(".js-arrow");
    jsArrow.onclick = function() {
    navLinks.classList.toggle("show3");
    }


    })( jQuery );

    This is how i enqueue.

    wp_enqueue_script( 'earn-ds-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), true );

    How should i resolve the problem for it to load?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator threadi

    (@threadi)

    Add the dependency here:

    wp_enqueue_script( 'earn-ds-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array( 'jquery' ), true );

    See: https://developer.ww.wp.xz.cn/reference/functions/wp_enqueue_script/

    Thread Starter shaibustephen

    (@shaibustephen)

    I appreciate your prompt response. I no more see any error message in browser console. But my challenge is that it does not trigger dropdown menu. Kindly view what i have done.. The dropdown work properly on the html but after integrating it to wordpress, it does not show. What am i getting wrong? What changes should i make?

    html

    <nav class="navigation">
    <div class="container">
    <a href="#" class="logo">Logo</a>
    <a href="#" class="mobile">&nbsp;</a><ul class="justify-content-end">
    <li><a href="#">Home</a></li>
    <li class="sub-icon"><a href="#">About</a>
    <ul style="opacity: 1; display: none;">
    <li><a href="#">School</a>
    <ul>
    <li><a href="#">Lidership</a></li>
    <li><a href="#">History</a></li>
    <li><a href="#">Locations</a></li>
    <li><a href="#">Careers</a></li>
    </ul>
    </li>
    <li><a href="#">Study</a>
    <ul>
    <li><a href="#">Undergraduate</a></li>
    <li><a href="#">Masters</a></li>
    <li><a href="#">International</a></li>
    <li><a href="#">Online</a></li>
    </ul>
    </li>
    <li><a href="#">Research</a>
    <ul>
    <li><a href="#">Undergraduate research</a></li>
    <li><a href="#">Masters research</a></li>
    <li><a href="#">Funding</a></li>
    </ul>
    </li>
    <li><a href="#">Something</a>
    <ul>
    <li><img src="https://placeimg.com/300/200/nature"></li>
    </ul>
    </li>
    </ul>
    </li>
    <li class="sub-icon"><a href="#">News</a>
    <ul class="normal-sub" style="opacity: 1; display: none;">
    <li><a href="#">Today</a></li>
    <li><a href="#">Calendar</a></li>
    <li><a href="#">Sport</a></li>
    </ul>
    </li>
    <li class="sub-icon"><a href="#">Contact</a>
    <ul style="opacity: 1; display: none;">
    <li><a href="#">School</a>
    <ul>
    <li><a href="#">Lidership</a></li>
    <li><a href="#">History</a></li>
    <li><a href="#">Locations</a></li>
    <li><a href="#">Careers</a></li>
    </ul>
    </li>
    <li><a href="#">Study</a>
    <ul>
    <li><a href="#">Undergraduate</a></li>
    <li><a href="#">Masters</a></li>
    <li><a href="#">International</a></li>
    <li><a href="#">Online</a></li>
    </ul>
    </li>
    <li><a href="#">Study</a>
    <ul>
    <li><a href="#">Undergraduate</a></li>
    <li><a href="#">Masters</a></li>
    <li><a href="#">International</a></li>
    <li><a href="#">Online</a></li>
    </ul>
    </li>
    <li><a href="#">Empty sub</a>
    <ul>
    <li><img src="https://placeimg.com/300/200/people"></li>
    </ul>
    </li>
    </ul>
    </li>
    </ul>
    </div><!--container-->
    </nav>

    WordPress

    <nav class="navigation">
    <div class="container">
    <a class="logo" href="#">Navbar</a>
    <ul id="menu-primary" class=" "><li id="menu-item-6" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-6"><a href="http://localhost/kaculat/sample-page/">Sample Page</a></li>
    <li id="menu-item-7" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-7"><a href="http://localhost/kaculat/sample-page/">Sample Page</a>
    <ul class="sub-menu">
    <li id="menu-item-8" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-8"><a href="http://localhost/kaculat/sample-page/">Sample Page</a></li>
    </ul>
    </li>
    <li id="menu-item-17" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-17"><a href="http://localhost/kaculat/sample-page/">Sample Page</a></li>
    </ul> </div>
    </nav>

    My Jquery

    (function($) {
    $(document).ready(function () {
    “use strict”;
    $(‘.navigation > ul > li:has( > ul)’).addClass(‘sub-icon’);
    $(‘.navigation > ul > li > ul:not(:has(ul))’).addClass(‘normal-sub’);
    $(“.navigation > ul”).before(“ “);
    $(“.navigation > ul > li”).hover(function (e) {
    if ($(window).width() > 943) {
    $(this).children(“ul”).stop(true, false).fadeToggle(150);
    e.preventDefault();
    }
    });
    $(“.navigation > ul > li”).click(function () {
    if ($(window).width() <= 943) { $(this).children(“ul”).fadeToggle(150); } }); $(“.mobile”).click(function (e) { $(“.navigation > ul”).toggleClass(‘show-on-mobile’);
    e.preventDefault();
    });
    });
    $(window).resize(function () {
    $(“.navigation > ul > li”).children(“ul”).hide();
    $(“.navigation > ul”).removeClass(‘show-on-mobile’);
    });// JavaScript Document

    })( jQuery );

    Moderator threadi

    (@threadi)

    The selectors in your jQuery don’t match the HTML code generated by WordPress. In the HTML code, there’s an additional element with the class .container between the element with the class navigation and the first <ul>. You need to account for this in your jQuery so that the events work.

    Incorrect:

    .navigation > ul > li

    Correct:

    .navigation > .container > ul > li

    You need to adjust this in all relevant places in the JavaScript-code.

    Thread Starter shaibustephen

    (@shaibustephen)

    I applied your solution and it messes up everything.

    complete css

    .navigation {
    width: 100%;
    background:#5d9fef
    }

    .navigation > .container > ul a.logo {
    display: inline-block;
    padding: 1.0em 1.7em;
    width: 19%;
    float: left;
    text-transform: capitalize
    }

    .navigation > .container > ul a.logo {
    text-decoration: none;
    color: #122f48;
    font-weight: 700;
    text-transform: uppercase;
    font-size: 20px;
    }

    .navigation > .container > ul .mobile {
    display: none;
    padding: 20px;
    }

    .navigation > .container > ul .mobile:after {
    content: "\f394";
    font-family: "Ionicons";
    font-size: 2.5rem;
    padding: 0;
    float: right;
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-25%);
    transform: translateY(-25%);
    }
    .navigation > .container > ul .sub-icon:before {
    content: "\f489";
    font-family: "Ionicons";
    display: none;
    cursor: pointer;
    float: right;
    padding: 1.0em 2em;
    background: #fff;
    color: #333;
    }

    .navigation > .container > ul > ul {
    margin: auto;
    width:auto;
    list-style: none;
    padding: 0;
    position: relative;
    box-sizing: border-box;
    }

    .navigation > .container > ul > ul:before,
    .navigation > .container > ul > ul:after {
    content: "";
    display: table;
    }
    .navigation > .container > ul > ul:after {
    clear: both;
    }
    .navigation > .container > ul > ul > li {
    float: left;
    background-color: transparent;
    padding: 0;
    margin: 0;
    }
    .navigation > .container > ul > ul > li a {
    text-decoration: none;
    padding: 1.0em 3em;
    display: block;
    color: #fff
    }
    .navigation > .container > ul > ul > li:hover {
    background: #f0f0f0;
    }
    .navigation > .container > ul > ul > li > ul {
    display: none;
    width: 100%;
    background: #f0f0f0;
    padding: 20px;
    position: absolute;
    z-index: 99;
    left: 0;
    margin: 0;
    list-style: none;
    box-sizing: border-box;
    }
    .navigation > .container > ul > ul > li > ul:before,
    .navigation > .container > ul > ul > li > ul:after {
    content: "";
    display: table;
    }
    .navigation > .container > ul > ul > li > ul:after {
    clear: both;
    }
    .navigation > .container > ul > ul > li > ul > li {
    margin: 0;
    padding-bottom: 0;
    list-style: none;
    width: 25%;
    background: none;
    float: left;
    }
    .navigation > .container > ul > ul > li > ul > li a {
    color: #777;
    padding: .2em 0;
    width: 95%;
    display: block;
    border-bottom: 1px solid #ccc;
    }
    .navigation > .container > ul > ul > li > ul > li a:hover{
    color:#03a9f4;
    }
    .navigation > .container > ul > ul > li > ul > li > ul {
    display: block;
    padding: 0;
    margin: 10px 0 0;
    list-style: none;
    box-sizing: border-box;
    }
    .navigation > .container > ul > ul > li > ul > li > ul:before,
    .navigation > .container > ul > ul > li > ul > li > ul:after {
    content: "";
    display: table;
    }
    .navigation > .container > ul > ul > li > ul > li > ul:after {
    clear: both;
    }
    .navigation > .container > ul > ul > li > ul > li > ul > li {
    float: left;
    width: 100%;
    padding: 10px 0;
    margin: 0;
    font-size: .8em;
    }
    .navigation > .container > ul > ul > li > ul > li > ul > li a {
    border: 0;
    font-size: 14px;
    }
    .navigation > .container > ul > ul > li > ul.normal-sub {
    width: 300px;
    left: auto;
    padding: 10px 20px;
    }
    .navigation > .container > ul > ul > li > ul.normal-sub > li {
    width: 100%;
    }
    .navigation > .container > ul > ul > li > ul.normal-sub > li a {
    border: 0;
    padding: 1em 0;
    }
    /* ––––––––––––––––––––––––––––––––––––––––––––––––––
    Mobile style's
    –––––––––––––––––––––––––––––––––––––––––––––––––– */
    .navigation .container {
    max-width: 1100px;
    }

    .site-header {
    position: relative;
    background: #fff;
    border-bottom: solid 1px #e9e9e9
    }

    @media only screen and (max-width: 959px) {
    .site-header {
    width: 100%;
    }

    .site-header .navigation > .container > ul {
    display:inline-block;
    }

    .navigation .container {
    max-width:100%;
    }

    .navigation > .container > ul .mobile {
    display: block;
    float: right;
    padding: 20px 20px 0;
    }
    .navigation > .container > ul .sub-icon:before {
    display: block;
    }
    .navigation > .container > ul > ul {
    display: none;
    width:100%;
    }
    .navigation > .container > ul > ul > li {
    width: 100%;
    float: none;
    display: block;
    }
    .navigation > .container > ul > ul > li a {
    padding: 1.0em;
    width: 100%;
    display: block;
    }
    .navigation > .container > ul > ul > li > ul {
    position: relative;
    padding: 0 40px;
    }
    .navigation > .container > ul > ul > li > ul.normal-sub {
    width: 100%;
    }
    .navigation > .container > ul > ul > li > ul > li {
    float: none;
    width: 100%;
    margin-top: 20px;
    }
    .navigation > .container > ul > ul > li > ul > li:first-child {
    margin: 0;
    }
    .navigation > .container > ul > ul > li > ul > li > ul {
    position: relative;
    }
    .navigation > .container > ul > ul > li > ul > li > ul > li {
    float: none;
    }

    .navigation > .container > ul > ul {
    clear: right;
    }

    .navigation > .container > ul .show-on-mobile {
    display: block;
    }
    }

    jquery

    /* global wp, jQuery / /*

    • File customizer.js.
      *
    • Theme Customizer enhancements for a better user experience.
      *
    • Contains handlers to make Theme Customizer preview reload changes asynchronously.
      */
      (function($) {
      $(document).ready(function () {
      “use strict”;
      $(‘.navigation > .container > ul > li:has( > ul)’).addClass(‘sub-icon’);
      $(‘.navigation > .container > ul > li > ul:not(:has(ul))’).addClass(‘normal-sub’);
      $(“.navigation > .container > ul”).before(“ “);
      $(“.navigation > .container > ul > li”).hover(function (e) {
      if ($(window).width() > 943) {
      $(this).children(“ul”).stop(true, false).fadeToggle(150);
      e.preventDefault();
      }
      });
      $(“.navigation > .container > ul > li”).click(function () {
      if ($(window).width() <= 943) { $(this).children(“ul”).fadeToggle(150); } }); $(“.mobile”).click(function (e) { $(“.navigation > .container > ul”).toggleClass(‘show-on-mobile’);
      e.preventDefault();
      });
      });
      $(window).resize(function () {
      $(“.navigation > .container > ul > li”).children(“ul”).hide();
      $(“.navigation > .container > ul”).removeClass(‘show-on-mobile’);
      });// JavaScript Document

    })( jQuery );

    Moderator threadi

    (@threadi)

    The code looks fine, though. I can’t figure out right now why it isn’t working the way you expect. Is there a link somewhere where I can see it?

    Thread Starter shaibustephen

    (@shaibustephen)

    is there anyway I can send an zipped copy of my work to you here. It is currently not online but on my local host

    Thread Starter shaibustephen

    (@shaibustephen)

    before your recommendation, I use it like below and work fine only that it does not drop-down

    .navigation > .div > ul > li

    Moderator threadi

    (@threadi)

    No, help in the forum takes place within the forum itself, not outside of it. You’re welcome to host the website you’ve created somewhere so that others can access it with a single click. You can also upload the source code to a site like https://gist.github.com – even though users will still have to copy it themselves – or as a repository on GitHub.

    However, your question primarily concerns the basics of HTML and CSS, not WordPress. You might find more help in a forum specializing in those topics. The generated HTML code with the menu likely comes from your WordPress theme, but your customization with the dropdown only applies to the resulting HTML code and isn’t specific to WordPress.

    That said, I’m wondering why you’re using such a custom jQuery solution for a dropdown in the first place. For two reasons:

    • These days, you can implement dropdown menus very easily with pure CSS. JavaScript is only needed in special cases, which I don’t see here so far.
    • Your WordPress theme might already include what you’re trying to build. Therefore, such customization might not be necessary at all. Since I’m not familiar with your project, I can’t say for sure.

    If you’re looking for personal support, you’ll surely find it here: https://jobs.wordpress.net

    Thread Starter shaibustephen

    (@shaibustephen)

    The issue is not HTML. I got it perfectly right but wordpress is providing wrong class. How do I remove current class associated with ul, ul ul, ul ul ul, li a etc in nav output in wordpress?

    Moderator threadi

    (@threadi)

    The HTML code for the menu depends on which theme you’re using. Depending on that, one can see how the HTML code might be adjusted. However, it would be easier for you to adapt your CSS and jQuery to the HTML code that is actually generated. As described above, you’ll need to adjust the selectors to do this. If my example doesn’t work for you, there must be another reason for it, which can’t be determined without a link to the page or the full source code as described above.

    Thread Starter shaibustephen

    (@shaibustephen)

        I used 'items_wrap'     => '<ul>%3$s/ul>', to remove default class associated with ul. How do i equally remove default class with <li></li> as seen below?
    	<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-2 current_page_item menu-item-6"><a href="http://localhost/kaculat/sample-page/" aria-current="page">Sample Page</a>after</li>
    Thread Starter shaibustephen

    (@shaibustephen)

    I was able to little work on the jquery. The issue now is that the jquery is not looking display none and block style on the ul ul. Kindly help me look at the jquery in my wordpress code and let me know what i have done wrong? I am equally seeking error message in my browser console

    Here is how i integrated the jquery

    wp_enqueue_script( 'kaculat-jquery-min', get_template_directory_uri() . '/assets/js/jquery.min.js', array('jquery'),  '2.1.3', true );




    (function( $ ) {

    "use strict";

    // javascript code here. i.e.: $(document).ready( function(){} );

    // on document ready
    $(document).ready(function () {

    // show/hide the mobile menu based on class added to container
    $('.menu-icon').click(function () {
    $(this).parent().toggleClass('is-tapped');
    $('#hamburger').toggleClass('open');
    });

    // handle touch device events on drop down, first tap adds class, second navigates
    $('.touch .sitenavigation li.nav-dropdown > a').on('touchend',
    function (e) {
    if ($('.menu-icon').is(':hidden')) {
    var parent = $(this).parent();
    $(this).find('.clicked').removeClass('clicked');
    if (parent.hasClass('clicked')) {
    window.location.href = $(this).attr('href');
    } else {
    $(this).addClass('linkclicked');

    // close other open menus at this level
    $(this).parent().parent().find('.clicked').removeClass('clicked');

    parent.addClass('clicked');
    e.preventDefault();
    }
    }
    });

    // handle the expansion of mobile menu drop down nesting
    $('.sitenavigation li.nav-dropdown').click(
    function (event) {
    if (event.stopPropagation) {
    event.stopPropagation();
    } else {
    event.cancelBubble = true;
    }

    if ($('.menu-icon').is(':visible')) {
    $(this).find('> ul').toggle();
    $(this).toggleClass('expanded');
    }
    });


    // prevent links for propagating click/tap events that may trigger hiding/unhiding
    $('.sitenavigation a.nav-dropdown, .sitenavigation li.nav-dropdown a').click(
    function (event) {
    if (event.stopPropagation) {
    event.stopPropagation();
    } else {
    event.cancelBubble = true;
    }
    });


    // javascript fade in and out of dropdown menu
    $('.no-touch .sitenavigation li').hover(
    function () {
    if (!$('.menu-icon').is(':visible')) {
    $(this).find('> ul').fadeIn(100);
    }
    },
    function () {
    if (!$('.menu-icon').is(':visible')) {
    $(this).find('> ul').fadeOut(100);
    }
    });

    });
    //# sourceURL=pen.js

    })(jQuery);

    Thread Starter shaibustephen

    (@shaibustephen)

    I was able to resolve the challenges after an intensive work. It triggers up downdown in mobile device within the @media (max-width: 991px) {} but doesnt show up in @media (min-width: 991px) {}. What do you suggest on how to get it work.

    .main-navigation li.clicked > ul {
    display: block;
    }
    .main-navigation li ul {
    display: none;
    position: absolute;
    min-width: 200px;
    max-width: 300px;
    padding: 0;
    margin: 0;
    }
    .main-navigation li ul a {
    background-color: #dfdfdf;
    font-size: 0.8125em;
    }
    .main-navigation li ul li {
    float: none;
    display: block;
    margin: 0;
    }
    .main-navigation li ul ul {
    left: 100%;
    margin-top: -40px;
    }


    @media (max-width: 991px) {
    .navbar .main-navigation ul {
    display: none;
    max-width: none !important;
    }
    .navbar .main-navigation li {
    float: none;
    display: block;
    margin: 0;
    padding: 0;
    }
    .navbar .main-navigation.kaculat > ul {
    display: block;
    position: absolute;
    margin: 0;
    background-color: #FFF;
    right: 0;
    left: 0;
    z-index: 100;
    padding: 0;
    }
    .navbar .main-navigation.kaculat > ul ul {
    display: none;
    width: 100%;
    z-index: 200;
    border: none;
    margin-left: 0;
    padding: 0;
    position: relative !important;
    }

    }

    My jquery


    jQuery(document).ready(function( $ ) {
    // show/hide the mobile menu based on class added to container
    $('.toogle-icon').click(function () {
    $(this).parent().toggleClass('kaculat');
    $('#hamburger').toggleClass('open');
    });

    // handle touch device events on drop down, first tap adds class, second navigates
    $('.touch .main-navigation li.nav-dropdown > a').on('touchend',
    function (e) {
    if ($('.toogle-icon').is(':hidden')) {
    var parent = $(this).parent();
    $(this).find('.clicked').removeClass('clicked');
    if (parent.hasClass('clicked')) {
    window.location.href = $(this).attr('href');
    } else {
    $(this).addClass('linkclicked');

    // close other open menus at this level
    $(this).parent().parent().find('.clicked').removeClass('clicked');

    parent.addClass('clicked');
    e.preventDefault();
    }
    }
    });

    // handle the expansion of mobile menu drop down nesting
    $('.main-navigation li.nav-dropdown').click(
    function (event) {
    if (event.stopPropagation) {
    event.stopPropagation();
    } else {
    event.cancelBubble = true;
    }

    if ($('.toogle-icon').is(':visible')) {
    $(this).find('> ul').toggle();
    $(this).toggleClass('expanded');
    }
    });


    // prevent links for propagating click/tap events that may trigger hiding/unhiding
    $('.main-navigation a.nav-dropdown, .main-navigation li.nav-dropdown a').click(
    function (event) {
    if (event.stopPropagation) {
    event.stopPropagation();
    } else {
    event.cancelBubble = true;
    }
    });


    // javascript fade in and out of dropdown menu
    $('.no-touch .main-navigation li').hover(
    function () {
    if (!$('.toogle-icon').is(':visible')) {
    $(this).find('> ul').fadeIn(100);
    }
    },
    function () {
    if (!$('.toogle-icon').is(':visible')) {
    $(this).find('> ul').fadeOut(100);
    }
    });

    });
Viewing 13 replies - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.