What is the best way to use Javascript to append a class to a dynamically generated child item in a Joomla menu, specifically within a designated div?

Currently, I'm in the process of designing a template using Bootstrap 4 specifically for Joomla 4. The menu structure on different levels such as parent, child, and deeper, is defined within the mod_menu php files. I've implemented the same main site menu in two positions: one for mobile devices (#sidenav) and another for desktop view (#mainmenu).

For li items at the child level that serve as dividers/headers for the next menu level (e.g. item 'More information' but not 'Top Level item'), I have utilized the .dropdown-submenu class from the mod_menu/default_separator.php file.

The sidenav is displaying as intended, but for the mainmenu div (desktop/large tablet view), I aim to have the second and subsequent level dropdowns appear as 'dropright'.

After manually adding the class using the Firefox inspector and seeing it work perfectly, I am now looking to automatically add this class using Javascript to change:

    <li class="dropdown-submenu">

to:

<li class="dropdown-submenu dropright">

Although my attempts with Javascript have been unsuccessful so far, here are some snippets I've tried:

function dropRight() {
    var elements = document.getElementsByClassName('.dropdown-submenu');
    elements.classList.add("dropright");
}

and

function dropRight() {
    var ul = document.getElementById('mainmenu');
    var li = ul.getElementsByTagName("li"); 
    for (let i = 0; i < li.length; i++) {
        if (li[i].classList.contains('dropdown-submenu')) {
            li[i].classList.add("dropright");
        }
    }
}

I've done extensive research and attempted various code combinations without success. As Javascript isn't my strong suit, any assistance or guidance would be greatly appreciated.

Answer №1

The solution has been discovered:

document.addEventListener("DOMContentLoaded", function(){showSubMenu()});
function showSubMenu() {
    var elements = document.getElementById("navigation").querySelectorAll("li.dropdown-sub");
elements[0].classList.add("drop-down-right");
}

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

JavaScript - Retrieve the name of an object from a separate array

Is it possible to dynamically map rows and columns in a table component using arrays of objects? For example, I have two arrays of objects like this: const columnData = [ { id: 'name', label: 'Name' }, { id: 'value', lab ...

Improved method for retrieving a subtask within a personalized grunt task?

As someone who is just starting out with Grunt and has only created a few custom grunt tasks, I've come up with what might be seen as an unconventional solution for traversing the initConfig to subtasks. My approach involves putting together a regex a ...

What is the best way to handle a form with nested components in React?

I have successfully developed a form that changes dynamically, incorporating my own components. However, I am facing the challenge of extracting information from this form because the select tag exists only in the children components. After some research, ...

I'm struggling to understand the folder arrangement and the system is telling me it can't locate my file

I'm having trouble identifying what might be causing issues with my file structure. Currently, I am using Aurelia for the front end and node for the server. I attempted a fix by performing a join operation, which resolved some of the problems. However ...

The jquery script for the dynamic remove button is malfunctioning

I have successfully implemented code to display dynamic controls using jQuery. Below is the working code: <script type="text/javascript"> $(document).ready(function() { $("input[value='Add']").click(function(e){ e. ...

What is the process for inserting a document into an array that is nested within another array?

My current dilemma revolves around a document (referred to as 'root') which contains an array of other documents ('stick'), each of which in turn contain another array of documents ('leaf'). Simply put: root{ stickChain[leaves ...

When trying to implement a dark/light theme, CSS variables may not function properly on the body tag

Currently, I am in the process of developing two themes (light and dark) for my React website. I have defined color variables for each theme in the main CSS file as shown below: #light{ --color-bg: #4e4f50; --color-bg-variant: #746c70; --color-primary: #e2 ...

What is the speed difference between calling functions from require's cache in Node.js and functions in the global scope?

Let's imagine a scenario where we have two files: external.js and main.js. // external.js //create a print function that is accessible globally module.exports.print = function(text) { console.log(text) } Now let's take a look at main.js: ...

Expanding the number of buttons for <ul> in creating a responsive navigation system using Angular

My navigation consists of 8 items (li), and as the resolution shrinks, the items are pushed onto a new line. I am looking to implement a feature where if an item doesn't fit in the navigation anymore, a "MORE" dropdown button appears on the right side ...

The class 'ConsoleTVsChartsCharts' could not be located

Attempting to implement Laravel charts using the package consoletvs/charts:6.*, Utilizing service providers ConsoleTVs\Charts\ChartsServiceProvider::class, With an alias: 'Charts' => ConsoleTVs\Charts\Charts::class, ...

Extract information from a JSON file to populate a jQuery Datatable

I am attempting to include data from a JSON file that was created using a Django script. Here is the structure of the JSON file: [ { "6": "yo1", "1": "2019-04-04", "4": "yo1", "3": "yo1", "2": "yo1", "5": "yo1" }, { "6": "yo2" ...

What is the advantage of using event.target over directly referencing the element in eventListeners?

Suppose there are several buttons in an HTML file and the following code is executed: const buttons = document.querySelectorAll('button'); buttons.forEach((btn) => { btn.addEventListener('click', (e) => { console.log(btn.te ...

Combining various data types within a single field in BigQuery

Is it feasible to specify a table schema with a field that allows for multiple data types? For instance: BIGQUERY TABLE SCHEMA schema: [{ name: 'field1', type: 'string', }, { name: 'field2', type: &apo ...

What is the best way to sort and organize this JSON data?

There is an array of JSON objects named events that contains fields such as eventId, eventName, and the date of the event. [{event1}, {event2}, {event3}]; The goal is to iterate through this array and filter out events that occur on the same day. The des ...

What is the procedure for turning off hover color on an href element?

Working on a website that utilizes MUI components, I have incorporated href into the Tab elements within the navigation bar. <Tab label={element} id={index} sx={{display: {xs: 'none', md: 'inherit'}}} href={`#${navElements[element ...

Combining Fields in Angular Filter to Enhance Search功能

I am facing a challenge with filtering a list of people based on their first and last name properties using Angular's filter functionality. The issue is that the filter only considers one property at a time, so users cannot search for both first and l ...

The hover effect does not carry over to duplicated HTML

I successfully added a node, but I forgot to include the hover function of the node in my application. The hover function is not necessary, and I need it to work with ie8 compatibility. Here's my HTML: <div id="appendCell" style="color:green; colo ...

What is the best way to reset a looping variable in a Node.js application?

I have encountered an issue with an API method in my nodejs app. Within one of my controllers, I am attempting to generate pages upon clicking. However, I have found that when calling the method, my for loop fails to reset the values to their default state ...

Using more than one class at a time is causing issues

Essentially, I have a div and I want to create an exercise where I apply three different classes using vue.js. I attempted to use v-bind:class, specifically :class, but can I bind a class directly from CSS? This is what I tried: html <div :style="[my ...

How to save data to a JSON file using the filesystem module in Node.js

After coming across this helpful guide at , I was intrigued by the creation of a point system in discord.js. The use of let points = JSON.parse(fs.readFileSync("./points.json", "utf8")); to read the file piqued my interest, leading me to explore how to bui ...