New action triggered by a click event following the prevention of the default behavior

Currently, the mobile menu on my test site (800px wide or less) does not have drop-down folder functionality. I am looking to make the mobile navigation menu function in the same way as it does on desktop.

Check out my website here

By default, when clicking on a title folder, the first link within that folder would open. To prevent this behavior, I used the following script:

<script>
$(document).ready(function() {
$('#mobileNav .mobile-folder>a').click(function(e) {
    e.preventDefault();
 });
 });
 </script>

Instead of opening the first link in the "folder titles," I want the hidden page links inside those folders to be displayed upon click.

However, my current code is not achieving the desired effect:

<script>
$(document).ready(function(){
$("#mobileNav .mobile-folder>a").click(function(){
$(this).find('.folder.external-link ul ').toggleClass("expand");
});
 });
  </script>

Here is the CSS I am using to hide and later display the page links:

.folder.external-link {display:none!important;}

.folder.external-link.expand {display:block!important;}

Any assistance on this matter would be greatly appreciated.

Answer №1

The element ul is actually a sibling of the element a, not its child.

https://i.sstatic.net/GTF7h.png

To correct your code, ensure it looks like this (and you may also retain the use of preventDefault()):

<script>
$(document).ready(function(){
    $("#mobileNav .mobile-folder>a").click(function(e){
       e.preventDefault();
       $(this).siblings('ul').toggleClass("expand");
    });
});
</script>

In addition, make sure your CSS targets the ul class properly:

.mobile-folder ul {display:none!important;}

.mobile-folder ul.expand {display:block!important;}

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

Is there a way to stop window.pageYOffset from resetting every time the page is rendered?

I've been working on a react component that changes the header based on the scroll event. By attaching an event handler and toggling a display class to show or hide certain elements depending on the scroll position. However, I've encountered som ...

Saving data for future mathematical calculations using JavaScript

Hi everyone, I have a challenging task at hand and I'm not sure if I'm on the right track, but here goes... I am developing a dynamic calendar and currently focusing on the functionality to go back a month when you click the arrow. I have a jQuer ...

Ways to Conceal/Deactivate Information in HTML

Welcome to my website mycareerpath.co.in. I am looking to remove the "FREE DOMAIN + 1GB HOSTING" from my site. Can anyone provide guidance on how to do this? Important Reminder Please remember to right click and inspect the page source for more informati ...

JTable MVC 4 - A problem arose during server communication

I am facing an issue with JTable and MVC 4. The data is not loading into the table and I am unsure why. The error message I receive is quite vague: An error occurred while communicating to the server. While debugging on the server side, I can see the data ...

css effect of background image transitioning on mouse hover

Is there a way to have an element on my webpage with a background image that follows the movement of the mouse when hovered over? I want it to be similar to this website: This is the HTML code I currently have: <section id="home" data-speed="3" data-t ...

Is it necessary to sanitize strings before assigning them as the content of a textarea element?

Consider the situation described below: var evil_string = "..."; $('#mytextarea').val(evil_string); Is it necessary to sanitize an untrusted string before setting it as the value of a textarea element? While I am aware of the importance of han ...

Adjust the size of a div element after updating its content using Mootools

I am currently utilizing mootools 1.2 on a basic webpage. There is a div that contains a form (referred to as formdiv) which is submitted through JS/ajax. Once the ajax request returns a "confirmation message" data, the formdiv updates accordingly. The is ...

Switching background images on click with a smooth transition

I'm in the process of creating a website and I want to be able to change background images with ease when clicked, like using fade in or fade out effects. The onclick functionality is already implemented with jQuery, but I'm having trouble incor ...

How to Combine Various Conditions in a ngStyle Directive?

I am working with a NgStyle Tag and would like to incorporate multiple conditions, but I am encountering issues where it doesn't work or produces errors. Here is the code snippet that I have been experimenting with: <div class = "card" [ngStyle] ...

Restricting Horizontal Scrolling in Dash DataTable with multi-tiered header (without any additional empty gaps)

I'm currently developing a Dash application using Plotly that showcases two tables, specifically dash_table.DataTable, each with multiple columns. The initial table features a multi-level header. For both tables, the first few columns are fixed while ...

How can I use absolute positioning and scrolling with an Iframe?

One of the key elements of this website is the implementation of iframes, with only one displayed at a time. However, my current issue revolves around the inability to scroll within these iframes due to their absolute positioning. I have attempted variou ...

Struggling to achieve consistent height for each div table box

Currently working on creating a table using CSS and divs, and I've got most of it set up except for the issue of uneven heights in the boxes. I've tried adjusting the heights and various commands, but to no avail. I'm unsure whether the prob ...

Tips for adjusting the position of the second child in my navigation menu using CSS

Struggling with customizing my navigation menu in CSS, I'm seeking assistance. My goal is to have the second child element of the navigation menu on a new line instead of below the first child (refer to the image for clarification). An example of the ...

Steps to redirect to an external page after successful login, even if it's not hosted on the server

I have developed an HTML5 app on my phone. Once the correct username and password are entered, the app is supposed to connect to an external server, authenticate the credentials in the database, and then redirect me to another page (Home Page) within the a ...

Issue with displaying list items in Ajax Accordion with CSS

I have implemented an accordion control extender on my webpage, and it is functioning properly. I added a CSS file to display it as a list, which is working perfectly in all browsers except for IE compatibility view. In IE compatibility view, the list-styl ...

Dividing a Price using Django Template Language

I am attempting to achieve a specific functionality in a Django Template Editor where I need to display a quarter of a price. The value quote.total is calculated by the system, such as 1235.00, and I want to manipulate this value to show 308.75 (which is ...

Occupying both horizontal and vertical space in a Bootstrap column

I have set up my buttons like this: https://i.stack.imgur.com/OMfhp.png In the image, you can see that the Left Option Button is larger, and I want all buttons to be like that. They are supposed to be aligned next to each other as I am using the Bootstra ...

Tips for stopping a CSS Grid overflow situation

My CSS layout is experiencing issues when filled with content. If I remove the 'p' tag from the HTML (inside a section with class "philosophy"), my layout works well - side margins appear as intended. However, when the 'p' tag is reins ...

The input field will display a border when it is actively selected

I am currently designing a form where the label should be positioned on the top border line of the text box. [![enter image description here][1]][1] Here is the code snippet: <div class="container"> <div class="row"> ...

Is it possible for a CSS menu with border to slide off the screen?

https://i.sstatic.net/bBbkU.png How can I create a CSS menu with borders that do not overflow off the page when using margin:0; I am trying to add a border to my CSS links without them overflowing on top of the page. I have imported my custom.css file in ...