Change the class of an element only within the div that is directly below it

Trying to achieve:

Visit this link

In my navigation menu, I have two folders with subpages under them. The CSS for displaying the page titles within the folder is as follows:

.main-nav .subnav ul {display:none;
transition: all 100ms ease-in-out;

}
.main-nav .subnav ul.expand {
display: block;
}

I am using jQuery to toggle the class .expand on click to show the titles:

<script>
$(document).ready(function(){

$(".main-nav li.folder").click(function(){
   $
$(".main-nav .subnav ul").toggleClass("expand");
 });
 });
   </script>

I am trying to modify it so that only the clicked folder's elements display instead of showing all folders at once.

Your help is appreciated. Thank you.

Answer №1

Your script is triggering the click event on every element with the class .subnav. You were so close to achieving your objective. Instead of adjusting/expanding the css for all classes, you should utilize $(this).

In my demonstration, I employ this approach to target all ".subnav ul" within the current element and then toggle the css accordingly.

$(".main-nav li.folder").click(function(){
   $(this).find('.subnav ul').toggleClass("expand");
});

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

You are unable to move the image to the top of the screen after zooming it with CSS

I implemented an image viewer component with interactive buttons for rotating, zooming in, and zooming out. Upon clicking a button, CSS transform is applied to the image. However, I encountered an issue where, when zooming the image, it cannot be scrolled ...

Can a border-bottom be made the same size as a class it is not linked to in CSS?

Can I set a border-bottom to match the size of a separate class? For example, I want a border-bottom at the bottom of each <section> tag. However, since the sections span the full width of the screen, the border-bottom inherits that width. Each < ...

Using jQuery: When the API has loaded successfully, execute the specified function

As I dive into working with a search flight API, my goal is to configure some settings when the API loads. Below is the code snippet for loading the API: skyscanner.load('snippets', '1'); function main(){ var snippet=new skysc ...

The unique GopikaTwo Gujarati font is unfortunately not compatible with Android mobile browsers and hybrid applications

I am currently trying to incorporate the custom font GopikaTwo into my hybrid application. Below is the code snippet I have added to my CSS file: @font-face { font-family: 'GopikaTwo' !important; src: url('GopikaTwo.eot') !impo ...

Verify if the text field is currently blank or has content before applying attributes

How can I disable one text box if another specific text box is filled within a div containing multiple text boxes? For example: When I enter text in textbox(4), I want to automatically disable textbox(1). And if I remove the text from textbox(4), I want t ...

What is the best way to define the width of text inside a div block?

I'm having trouble adjusting the width of the text in my code to only fill 80% of the body element. I've tried using both the width property and padding, but neither seems to be working for me at the moment. Here's the HTML: <body&g ...

Exploring the World of MVC 4: Delving into Modal Windows, Partial Views,

I am currently utilizing MVC4 along with Entity Framework to build a web application. Within my database, I have a table listing all the individuals. Each person can have their information edited through a modal window that is implemented as a Partial View ...

How to delete a specific element from the DOM using its ID in JavaScript

I am facing a challenge in removing an element from the page dynamically without refreshing using JavaScript. The script I have written successfully deletes the record from the database, but I need assistance in removing the container element based on it ...

JavaScript has encountered a syntax error

When working on an animation in javascript, I encountered a problem that I can't seem to identify. I am attempting to make the pan function work with the "mover" function, but it seems like either I am not using the properties correctly within the "tr ...

php not writing on initial attempt

I have been storing some data in local storage variables and I want to transfer them to a file created using PHP. On my HTML page, there is a function named updatesurvey. This function performs the following tasks: var newDate = new Date(); var ...

Troubleshooting: JQuery dropdown selection not updating image display

I am trying to create a select menu that changes the car image when a user selects a different model, but for some reason it is not working. Here is what I have tried: <h2 class="model">A6 <img src="images/3.jpg" id="image" width="544" height="2 ...

Displaying an error message: Uncaught ReferenceError - Undefined reference detected

Hi there, I am having some trouble with a JSON file and I would appreciate some help. The error message I am receiving is: Uncaught ReferenceError: marketlivedata is not defined <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquer ...

Retrieve JSON data from a server using jQuery following the submission of a form

Similar Question: JQuery Ajax Return value After filling out a login/register form, the server sends me to a JSON page. I am trying to retrieve that JSON data into a JavaScript/jQuery file without being redirected to that page. Is there a way to acce ...

Creating a counter for a list using CSS

I am attempting to utilize the counter increment feature in CSS for my ordered list, but it doesn't seem to be functioning properly. Here is the desired format I want to achieve: 1. Acknowledgements 1.1 blah, blah .... 1.2 blah, blah .... 1 ...

Adjust the pagination length within the jQuery DataTables plug-in

I am looking to customize the pagination length in DataTables plug-in for jQuery. Specifically, I want to calculate the number of pages on the server side and display the correct amount of buttons on the client side. Can anyone provide guidance on how to ...

Placing the video at the center of the background image

                    Can someone assist me with a design issue I'm facing? I have two divs within a section. The left div contains a heading and a button, while the right div contains a video. However, when I try to add a background image to ...

How can we prevent the UL scroll animation from restarting in jQuery?

I've implemented a jQuery code with an AJAX function that updates 2 UL lists whenever the data on a WordPress page is changed. While the functionality works as expected, I'm facing an issue where the scroll animation restarts from the first LI e ...

Guide on populating a series of rectangles in a line based on values stored in an array using d3.js

I have 100 rectangles arranged in a 10x10 square. My goal is to assign colors to the rectangles based on values from an array var avg = [1, 4, 4, 7, 11, 15, 58] I'm facing an issue at the value 4 being repeated and I find the current code quite mess ...

The Safari browser displays the mobile-friendly version of the website

Everything appears correctly when looking at the website in Firefox and Chrome. However, Safari is displaying the mobile version of the site instead. I did not make any CSS changes specifically for desktop screens while working on the responsive design for ...

Using multiple selectors in JQuery and Javascript

I have a challenge where I need to execute different actions for specific divs. Here is the code snippet I currently have: $("#pending-cancel-1, #pending-cancel-2").click(function(){ Do something special for pending-cancel-1 and pending-cancel-2... }) ...