Seeking modifications to the CSS in order to enable functionality of the megamenu

I recently worked on a SharePoint project where I had to create a global navigation sourced from a SharePoint list. The implementation was successful, and I created a mock-up that can be viewed here.

While w3schools provided a static version, I utilized DOM manipulation to make it dynamic using an array of objects to simulate a SharePoint list.

One particular challenge I encountered was with creating a mega-menu for the "Directorate" menu item. I merged the CSS of the Navigation with the CSS of the mega-menu, causing issues. The problem seemed to arise when adding classes to elements in the loop through the menu items from the array. Specifically, giving the mega-menu item different class names 'Mdropdown' and 'Mdropbtn' caused conflicts.

CreateNavigation(navData) {
 // Existing code...
}

function buildSubNavBar(subNavID, isDirectorate) {
  // Existing code...
}

I have shared a CodePen link for better clarity. Changing line 115 to `ddDiv.classList.add("Mdropdown")` and line 118 to `btn.classList.add("Mdropbtn)` helped resolve the issue with the mega-menu but caused problems with other menu items.

In addition to this challenge, I added categories and sub-categories to further enhance the mega-menu functionality. Building the mega-menu involved complex interactions between various category elements within the menu.

If you have any suggestions or solutions to address the conflicting class names and improve the overall functionality of the mega-menu, please share them. Your assistance is greatly appreciated!

Answer №1

Revise the buildSubNavBar function as shown below.

function updateSubNavMenu(subNavID, isDirectorate) {
    //create a div and apply dropdown CSS class
    var ddDiv = _createEl("div");
    if(isDirectorate === "directorate"){
        ddDiv.classList.add("Mdropdown");
    }else{
        ddDiv.classList.add("dropdown");
    }
    
    //create a button element and set text
    var btn = _createEl("button");
    if(isDirectorate === "directorate"){
        btn.classList.add("Mdropbtn");
    }else{
        btn.classList.add("dropbtn");
    }
    var btnText = document.createTextNode(subNavID);
    //append the text to the button
    btn.appendChild(btnText);

    //create an i tag with classes "fa fa-caret-down"
    var itag = _createEl("i");
    itag.classList.add("fa");
    itag.classList.add("fa-caret-down");
    btn.appendChild(itag);
    ddDiv.appendChild(btn);

    var ddContent = _createEl("div");
    ddContent.classList.add("dropdown-content");
    for (var i = 0; i < subList.length; i++) {
        if (subList[i].id === subNavID  && subList[i].id !== "Directorate") {
            var li = _createEl("li");
            var a = _createEl("a");
            var aTextNode = document.createTextNode(subList[i].URLNAME);

            a.href = subList[i].subURL;
            a.appendChild(aTextNode);
            ddContent.appendChild(a);  
        }
    }
    
    if(isDirectorate === "directorate"){
        //create another dropdown menu
        var megaDivDropDown = _createEl("div");
        megaDivDropDown.classList.add("dropdown");

        var megaBtn = _createEl("button");
        megaBtn.classList.add("dropbtn");

        megaDivDropDown.appendChild(megaBtn)
        var megaI = _createEl("i");
        megaI.classList.add("fa");
        megaI.classList.add("fa-caret-down");

        megaBtn.appendChild(megaI);

        var megaDDivContent = _createEl("div");
        megaDDivContent.classList.add("Mdropdown-content");

        var headerDiv = _createEl("div");
        headerDiv.classList.add("Mheader");

        var megaH2 = _createEl("h2");
        var h2Text = document.createTextNode("Team Sites");

        megaH2.appendChild(h2Text);
        headerDiv.appendChild(megaH2);
        megaDDivContent.appendChild(headerDiv);

        var megaDDivRow = _createEl("div");
        megaDDivRow.classList.add("Mrow");

        //Iterate through categories and sub-categories items

        for (var i = 0; i < megaMenuCategory.length; i++) {
            var megaDivCol = _createEl("div");
            megaDivCol.classList.add("Mcolumn");
            var colHr = _createEl("h3");
            var colHrText = document.createTextNode(megaMenuCategory[i].category);
            colHr.appendChild(colHrText);
            megaDivCol.appendChild(colHr);
            for (var x = 0; x < categoryMenu.length; x++) {
                if (megaMenuCategory[i].category === categoryMenu[x].category) {
                    var colAnchor = _createEl("a");
                    colAnchor.href = categoryMenu[x].menuUrl;
                    var menuColText = document.createTextNode(categoryMenu[x].menuItem);
                    colAnchor.appendChild(menuColText);

                    megaDivCol.appendChild(colAnchor);
                    megaDDivRow.appendChild(megaDivCol);
                    megaDDivContent.appendChild(megaDDivRow);
                    ddDiv.appendChild(megaDDivContent);
                }
            }
            //add more elements
            console.log("megaDdivRow:")
            console.log(ddDiv);
        }
    }
    ddDiv.appendChild(ddContent);
    return ddDiv;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Detect the initial collision exclusively (Collision detection using raycasting)

I am currently working on a never-ending runner game where a character is positioned at (0,0) and can only move along the X-axis by using arrow keys or the mouse. There are two different types of objects moving from z = -10000 towards the character, and on ...

The positioning of the parent element shifts as a result of the CSS

What causes a vertical position change in buttons with content floated left or right? Take this example http://jsfiddle.net/8ff6dhou/ <button>aaa</button> <button><div style="float:left">bbb</div></button> <button> ...

Select three random items from a string array list along with their corresponding indexes using TypeScript in Angular

Consider this example: I am working with a string array const groceries = [ 'milk', 'coriander', 'cucumber', 'eggplant', 'carrot', 'brinjal', 'on ...

Using the "this" keyword in JavaScript to access the "rel"

Take a look at the JSFIDDLE , where you will notice that the rel attribute in the alert is shown as 'undefined' : var ItemTypeArray = $('input[name^=ItemType]:checked').map(function(){ alert(this.id + ' , r= ' + this.rel) ...

The result of jQuery is an Object Object in a dynamic and user-friendly web application

Working on an application that requires the use of JQuery has brought up an issue with my functions related to sons and grandsons in the code below: When typing http://localhost:8080/fathers into my browser, I receive the following response: [{"id":1," ...

Utilize a Google Chrome extension to interact with the DOM of the active tab

Alright, so I've got a good grasp on JavaScript/jQuery and can use them for various tasks. However, my current challenge involves manipulating the DOM of the open tab. I'm curious to know if it's feasible or if all the actions performed by a ...

Route.post() is in need of a callback function, however, it was provided with an [object String

I've been working on developing my own vocabulary app by following and modifying the MDN node/express tutorial. While the MDN tutorial runs smoothly, I'm encountering issues with my version. Below are the errors I'm facing: Error: Route.p ...

Utilizing jQuery functions within Vue components in Quasar Framework

I've recently started delving into web app development and I'm encountering some basic questions regarding jQuery and Vue that I can't seem to find answers to. I have an application built using the Quasar Framework which frequently involves ...

How come I'm unable to showcase the inventory of items to the customer even though they are saved in the database and visible in the Network tab on the console?

Basically, my goal is to showcase products on the client side. I am using Angular 12 with .NET 5 for the backend. Although I am fetching products from the product's controller, they are not visible to me. However, I can see the "welcome" title and I k ...

Iframe overlay feature functioning on Chrome but not on IE11

I have a Document viewer with a .less file containing the following styling: div.document-previewer-container { //height: 400px; //width: 300px; position: absolute; top: 0; bottom: 0; left: 0; right: 0; //padding: 5px 2px; > div.document-preview { h ...

Is there a way to alter the background color of a Material UI card when it is being hovered over

I'm currently using material ui with react and I have a question regarding the background color of my card component when a user hovers over it. Can someone help me figure out how to achieve this? For reference, here is the live code link in CodeSand ...

The functionality of Regex in JavaScript is not meeting my expectations

I've come up with a regular expression for Name validation that only allows characters like "_", "-", "'", and "." Here is the regular expression pattern: ^[a-zA-Z][a-zA-Z0-9\.\-_']{1,24}$ The issue is that it's allowing na ...

The system is currently unable to find the specified element

I am facing an issue trying to locate a button that is defined under a specific class using XPATH. The error message "Unable to locate element" keeps popping up. Here are the details of the class: <div class="aui-button-holder inputBtn" id="aui_3_4_0_1 ...

collection of random header images for a ruby on rails website

Can anyone help me with an issue I'm having? I am trying to randomize images in a header on a category page using the following code, but it doesn't seem to be working. I keep getting this error: Invalid CSS after "...ackground: url(": expected " ...

Strangely behaving animated mobile navigation glitch

CodePen showcasing the mobile navigation Upon initial interaction with the mobile navigation, it operates as expected. However, a bug arises with subsequent interactions. The navigation either opens and closes instantly or the links appear in a jumbled or ...

Can we create a collection of Vue highcharts components in a library format?

I am in search of an answer, with hope that it lies here. I have used the following: "vite": "^2.7.13", "highcharts": "^9.3.3", "highcharts-vue": "^1.4.0" I aim to create a library of Vue compon ...

What steps do I need to follow in order to perform a particle design simulation on my laptop

Recently, I attempted to utilize some of the particle designs featured on this website https://speckyboy.com/particle-animation-code-snippets/, but encountered difficulties. I have included both the stylesheet and javascript files, yet the design does not ...

The parameter of type 'X' cannot be assigned to the argument of type 'Y'

I've defined the following types: export type Optional<T> = T | null; along with this function updateBook( book: Array<Optional<Hostel>>) which I'm using like this let book: Hostel | null [] = [null]; updateBook(book) Ho ...

Issue with IE7 when using JQuery to repopulate a <ul> unordered list: new elements showing up under previously hidden elements

Within this javascript snippet, I am utilizing the code below to clear out a list of countries within a <ul> element and then repopulate it (with a slight animation using jQuery's hide() function). The functionality works smoothly in Chrome and ...

What is the best way to generate a complete PDF of a webpage using pdfmake?

I'm currently developing a web application and facing the task of converting an HTML page, which contains multiple tables and datatables, to a PDF format. To achieve this, I've chosen to utilize the library pdfmake. Below is the script that I ha ...