Get these click functionalities functioning without relying on jQuery

I have designed a vertical menu with multiple sub menus. Each menu contains at least 3 sub menus, and the sub menus are set to open only when their parent menu is clicked. (I require jQuery support for this menu) However, since I do not use jQuery on any other pages, I decided not to link jQuery for this particular menu. Without jQuery, the menu appears like this: withoutjQuery

**If a coder can make this menu work without jQuery, you are welcome to do so. Alternatively, suggestions on how to achieve this using just CSS are also appreciated.

Here is the jsfiddle that utilizes jQuery. Can someone modify it to work without jQuery?

Function

  $(function () {
      $('li ul').hide();
      $("#leftmenuidfrmslt li").click(function(e) {
          var submenu = $(this).children("ul");
          if (!$(submenu).is(":visible")) {
              $('li ul').slideUp("fast");
              submenu.slideDown("fast");
          } else {
              submenu.slideUp("fast");
          }
      });
  });

Answer №1

My suggestion involves implementing a solution without the use of jQuery. By adding specific classes to your CSS and applying them to the appropriate UL-tags in your HTML code, you can achieve the desired functionality. Check out this jsfiddle sample that demonstrates how to accomplish this using pure JavaScript (no jQuery).

function getXmlHttp(){
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (ee) {
        }
    }
    if (typeof XMLHttpRequest!='undefined') {
        return new XMLHttpRequest();
    }
}
function hide(node){
    node.style.display='none';
}
function func(e){
    e.preventDefault();
    var q = getXmlHttp();
    var url = this.getAttribute('href');
    q.open('GET', url , false);
    q.send(null);
    var result = 'File '+url+' not found.';
    if(q.status == 200) {
        result = q.responseText;
    }
    document.querySelector('.page').innerHTML = result;
    document.querySelector('#leftmenu').style.width = '70px';
    var items = document.querySelectorAll('li ul, #leftmenu b');
    for (var i=0; i<items.length; i++) {
        hide(items[i]);
    }
    this.removeEventListener('click',func(),false);
}

function slideUp(node){
    node.setAttribute('class', "submenu hidden");
}

function slideDown(node){
    node.setAttribute('class', "submenu visible");
}

function switchSubMenu(){
var ch = this.children;
    for(var i=0; i<ch.length; i++) {
        if (ch[i].nodeName == 'UL') {
            var v = document.getElementsByClassName("visible");
            if (v) {
                for(var j=0; j<v.length; j++){
                    if(v[j] != ch[i]) {
                        slideUp(v[j]);
                    }
                }
            }
          ch[i].getAttribute('class') == "submenu hidden" ? slideDown(ch[i]) : slideUp(ch[i]);
        }
    }
}

var sm = document.querySelectorAll('.submenu');
for(var i=0; i<sm.length; i++) {
    slideUp(sm[i]);
    sm[i].parentNode.addEventListener("click",switchSubMenu, false);
}
var links = document.querySelectorAll('.reveal');
for(var j=0; j<links.length; j++) {
    links[j].addEventListener("click",func, false);
}

Answer №3

Include the font size settings as shown below

.small-icon { 
    font-family:"Flaticon";
    font-size: 20px;
    line-height: 1;
}
.small-icon:before {
    font-size: 20px;        
    margin-left: 0;
}
.big-icon { 
    font-family:"Flaticon";
    font-size: 40px;
    line-height: 1;
}
.big-icon:before {
    font-size: 40px;        
    margin-left: 0;
}

When adding it to your HTML file

<div class="center boxborder">
    <ul class="social">
        <li><a href="#"><span class="small-icon flaticon-facebook12" style="color: #3399ff;"></span></a>

        </li>
        <li> <a href="#"><span class="small-icon flaticon-social19" style="color:#33ccff;"></span></a>

        </li>
        <li> <a href="#"><span class="small-icon flaticon-google109" style="color: #ff3300;"></span></a>

        </li>
        <li><a href="#"><span class="small-icon flaticon-linkedin11" style="color: #21a6d8;"></span></a>

        </li>

    </ul>
</div>

Answer №4

While CSS technically allows you to achieve this, utilizing jQuery or vanilla JS is recommended for a more robust solution.

By using JavaScript, you can ensure a proper separation of concerns between content (HTML), styling (CSS), and functionality (JS).

However, if you prefer to explore CSS options, you can come close with the following code snippet:

Check out the Demo on Fiddle

HTML

<div class='menuGroup'>
    <label id='item1'>
        <input type='radio' for='item1' name='menu1' />Item 1
        <div class='menuGroup'>
            <label id='subitem1'>
                <input type='radio' for='subitem1' name='submenu1' />Subitem 1
                <div class='menuGroup'>more menu levels...</div>
            </label>
            <label id='subitem2'>
                <input type='radio' for='subitem2' name='submenu1' />Subitem 2
                <div class='menuGroup'>more menu levels...</div>
            </label>
        </div>
    </label>
    <label id='item2'>
        <input type='radio' for='item2' name='menu1' />Item 2
        <div class='menuGroup'>
            <label id='subitem3'>
                <input type='radio' for='subitem4' name='submenu2' />Subitem 3
                <div class='menuGroup'>more menu levels...</div>
            </label>
            <label id='subitem4'>
                <input type='radio' for='subitem5' name='submenu2' />Subitem 4
                <div class='menuGroup'>more menu levels...</div>
            </label>
        </div>
    </label>
</div>

CSS

label {
    display:block;
    position:relative;
}
label:not(:last-child) {
    border-bottom:1px solid grey;
}
input {
    display:none;
}
input:checked+.menuGroup {
    display:block;
    width:200px;
    left:100%;
    top:-1px;
}
label .menuGroup {
    display:none;
}
.menuGroup {
    position:absolute;
    border:1px solid grey;
}

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

Locating all elements on a webpage that are either above or below a specific element, capturing those that intersect visually

Is it possible to locate all the additional HTML elements that a particular element overlaps with, is positioned under, or intersects with? My goal is to ensure that this element is displayed above every other element. I am looking to identify all interse ...

I created a design for a subscription form that appears aesthetically pleasing in Firefox, however, it does not maintain the same appearance in Opera. Any suggestions on how I can resolve this issue?

Why does my code display differently in Firefox compared to Opera? In Firefox, the height of input/text and input/submit elements are similar. However, in Opera, the height of the input/submit is reduced. This is the HTML code: < ...

Changing the color of a div while implementing a show and hide effect in JavaScript

I have designed a checkout system with three distinct parts - shipping information, billing information, and order confirmation. These sections are all on the same page, allowing for a seamless flow during the checkout process. Once a customer completes t ...

Sending a concealed input according to the chosen option

I'm attempting to send some hidden values to a Servlet via a form, but my goal is to only pass them if the user chooses a specific option. <!-- FORM ABOVE --> <input type="hidden" name="foo" id="foo" value="foo"> <input type="hidden ...

Is the code generated by Webflow.com functional and practical?

Recently, I have discovered the wonders of www.webflow.com for creating an admin layout. It excels in generating a flexbox layout based on my PSD design without requiring me to manually code with Gulp. My plan is to further simplify the process by breaki ...

Expanding the width of a list-group with Bootstrap to fill the entire div

I am working with a div that has a height of 200px. How can I make sure the ul items are divided into equal rows within the div, instead of always aligning to the top? To illustrate this, I have colored the div green. As I will be using ngFor in my ul, the ...

Displaying an Array in HTML using Angular

Just starting out with Angular and experimenting with data display from an array. The data in the array is: [Me, Three, Four] I attempted to loop through it for printing but encountered issues. Here's a snippet of how I'm currently handling it: ...

Access a particular html tag as a value within an object

Recently, I've been working on AngularJS version 1.6 and am faced with an API containing multiple objects structured similar to the example below: { "description": " <p><a href=\"url">link</a>text</p><p><a href ...

Is there a gsub feature available in awk for applying color to characters on unix systems

I have a script that generates an output.txt file. The script contains 4 columns shown below: Filename Date Space status xyz.txt Nov18 3.8M On_time mnp.txt Nov11 8.7M Delayed pqr.csv Nov16 9.0M No_records I automate sending the output file via email, and ...

Retrieve data from computed attributes

When attempting to retrieve the value of an input box, I encountered a situation where the value displayed on the webpage did not match what was present in the HTML tag (which only showed "---"). Here is the HTML tag along with a link to a reference image: ...

How come the Bootstrap 4 column is not utilizing its entire available width?

My webpage is structured as follows: <div class="container"> <div class="row mb-5"> <div class="col-12 col-md-3"> <img class="mr-5 img-fluid" src="big_icon_1.png" alt="Big icon 1"> </div> <div class-"co ...

A critical bug has been discovered in Safari and Chrome where HTML5 canvas fails to work in a specific scenario, while Firefox performs perfectly

I am currently using html5 canvas to develop a function-graph software. There seems to be a critical bug that I cannot figure out. For example, when I use the following code: yOFxGraph(plotFUNCinputYofX,-4,5,-4,4,"rgb(66,44,255)",1); everything operates ...

The table header does not stay in place or remain fixed

My issue lies in using bootstrap, HTML, and CSS. As I scroll, the table header does not move along with the data. I've attempted implementing sticky-top and position-relative without any success. <div class="tab-div"><table class="table tab ...

Conditional rendering of a component in ReactJS while maintaining the "empty" space

Is there a way to conditionally render a component without affecting the layout of other components? I'm trying to avoid unwanted shifts caused by this div https://i.sstatic.net/g1eUd.gif Do you have any suggestions? Here is a snippet of my code: ...

Switching FontAwesome Stacks on Hover

I'm facing a challenge with creating a quick animation that replaces an entire span on hover. How can I successfully approach replacing a span on hover? <h1>I am looking to have this element replaced...</h1> <span class="fa-stack fa-lg ...

Setting custom domains on Heroku is essential for optimizing the performance and branding of my website

Essentially, when using Heroku, you are provided with a default domain: XXX.herokuapp.com. Personally, I have a collection of REST APIs that I want to configure on a domain called: api.myDomain.com. At the same time, I also have my HTML files (web view) ...

Ways to display content alongside an image

Is there a way to verify if an application is running on another server by displaying an image it provides? The challenge is that altering the content of the image is not an option. If the image is not provided, can I show an alt attribute indicating that ...

Stop input-group-append from causing adjacent input-groups to shift right

When I have two input fields next to each other and render a button within an input group, the button pushes the neighboring input field on the right, decreasing its width. This behavior can be observed in this example sandbox: view code sample. To illus ...

Adjusting Window Size Causes White Space to Appear Above Div

I currently have two inline-block div in my post page that showcase my latest book reviews. Typically, these sections align perfectly and occupy the same amount of space on the screen. However, for certain window sizes, one of the inline-block elements end ...

Incorporate the content of an HTML file into a Django template by substituting a portion

I am encountering an issue with replacing a portion of a website that was created using Django with different content. The code snippet in question looks like this: <section id='main_page'> <div id="main_div"> <--! inc ...