Toggle the visibility of items by clicking on them

I am looking to toggle the visibility of elements on a timeline based on the selected year.

For example, if a user clicks on the year 2015, all child elements should be hidden. If the user clicks on the same year again, the elements should then be shown.

I have customized the HTML and CSS for my design, with the original script available at

http://codyhouse.co/gem/vertical-timeline/

I've created a demo on JSFiddle here: http://jsfiddle.net/6nvumkxc/2/

However, I'm encountering an issue where clicking on the year only hides the first element instead of all the cd-timeline-block within that specific year block.

Answer №1

The reason for this behavior is that using .next() only targets the first next sibling in the current context, not all of them. To target all siblings, you can modify the click handler like so:

$('.cd-year').click(function(){
  $(this).parent().find('.cd-timeline-block').slideToggle();
});

Alternatively, you can use:

$('.cd-year').click(function(){
   $(this).nextAll().slideToggle();
});

or

$('.cd-year').click(function(){
    $(this).siblings().slideToggle();
});

See Working Demo

Update: To keep only the first year option open:

$('.cd-year').click(function(){
   $(this).nextAll().slideToggle();
}).not(':first').click();

Demo with first option open

Answer №2

Make sure to implement the following code snippet:

$('.cd-year').click(function(){
    $(this).nextAll(".cd-timeline-block").slideToggle();
});

It is important to note that .next() will only retrieve the next element, whereas you need to utilize .nextAll() to retrieve all elements.

Check out this Fiddle for reference

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

The <HTML> element is just a bit too big for the screen

When viewing my webpage on mobile, the video background does not fit the screen properly (it fits in landscape orientation). Here is the video tag I am using: <video autoPlay loop muted playsInline className='absolute w-full h- ...

What is the best way to change the CSS style of a class using jQuery?

Struggling to make changes to a specific class style without success. I am aware of the addClass function in jQuery for adding a class, but not sure how to override just the style of a class. Is there a method to override styles directly? ...

The challenge of applying flex property in React Native JSX

I'm facing a challenge while trying to implement form field validation in various forms within a React Native mobile application. The problem arises when the error message, which should appear below the text input field, ends up occupying half of the ...

Getting a 406 (Not acceptable) error when trying to perform an AJAX action in Rails 4

I am attempting to automatically refresh a specific partial view every 60 seconds on the homepage. To make it easier to manage, I have divided the actions into two routes. However, I am encountering an issue with the respond_to block and could use some ass ...

Angular Material has support for displaying nested optgroups within select components

Upon reviewing a question on Stack Overflow, I learned that nested optgroups are not supported in the HTML5 spec. While it is possible to manually style them, I am curious if Angular Material offers a solution. Angular Material internally swaps many eleme ...

The Wordpress sidebar is positioned at the bottom of the page's main content

After going through various posts related to this issue, I still can't find a solution that applies to my case. Here is the link to the blog.css file: http://pastebin.com/3P71GtRT You can also visit the URL I am struggling to position the sidebar ...

Struggling to center a fixed width div horizontally in Internet Explorer

Currently in the process of creating a website template. To see an example, visit Framework Login Page The primary CSS file can be found here: master.css The centralizing of the main parent div is proving to be a challenge. This is the code I am using ...

Drawbacks of adjusting design according to screen width via javascript detection

When creating a website, I want it to be compatible with the most common screen resolutions. Here is my proposed method for achieving this: -Develop CSS classes tailored to each screen resolution, specifying properties such as width and position. -Write ...

Using Javascript Regular Expressions to validate that the first number in an amount is non-zero

Need a solution to only accept numbers that do not start with zero, but can contain zeros after the first digit. Currently using var.replace(/[^1-9]/g, ''); which prevents input of 0 altogether. Examples of valid inputs: 10 9990 Examples of in ...

Obtain array buffer from NodeJS to capture frames from a video file

My current goal is to extract frames from a video in node without relying on a video tag. I am utilizing openvg-canvas for this task, which allows me to print images and use the canvas API functionalities successfully. The challenge lies in the fact that ...

The close button within the modal is malfunctioning even with the implementation of the on() function

My "show more" button triggers the modal to pop up, but when I click on the X button in the top left corner, it doesn't close as expected. However, clicking outside of the box or pressing the "esc" key does work. I suspect that the issue lies with th ...

What is the process for embedding JQuery within the <body> tags in Joomla 3.1?

I inserted the following code into my default.php file in Joomla 3.1. <?php JHtml::_('jquery.framework'); JFactory::getDocument()->addScript(JURI::root().'template/mytemplate/js/jquery.min.js'); ?> However, this code only pl ...

I'm seeking guidance on handling complex relationships with Mongoose. Please provide a specific challenge example for a quick solution. Thank you

While I am skilled in using sequelize with node, angular, express etc., I am just beginning to delve into the M part of the MEAN stack by learning mongoose. However, I'm unsure of MongoDB's capabilities when it comes to organizing data in the dat ...

The tooltip feature for icon buttons within Material UI list items is not functioning properly as anticipated

Just starting out with Material UI and React, I've encountered a strange UI problem that I can't quite figure out. Hopefully someone here can help me identify what I did wrong. My Approach: I have a List in my code where each list item has butto ...

Putting off the execution of jQuery scripts

For my simple website, I incorporated the jScrollPane plugin to create a centered, fixed div with a height of 600px. Everything was functioning smoothly until I added a Facebook comment section, causing the scrollable div not to wait for the comment block ...

What is the best way to bring HTML files into the dist directory?

When working in my development environment, I utilize the following code: res.sendFile(path.resolve(__dirname,'../Views/setpwd.html'); It functions correctly within the development environment. However, when moving to production, the Views are ...

Tips for consolidating an array of numbers with close values

Looking to develop a function that can shrink a list of numbers based on a specified variation. Variation Explanation: The variation should cover a range above and below a given value. For example, if the variance is set at 100 for a value of 5460, it sh ...

The Angular currency filter is displaying a strange blank space despite the correct syntax being used. Is there anyone who can assist me with this issue?

Hey everyone, I'm having some trouble with the currency filter in Angular. It's not working and just displaying a blank space. I'm new to learning Angular so any help would be greatly appreciated! Here is the code I'm using: <!DOCT ...

Merging values of different keys in a .json file

Check out my json file below: { "foo": "https://3a1821d0.ngrok.io/api/foo", "bar": "https://3a1821d0.ngrok.io/api/bar", } I am interested in changing the key 3a1821d0 to a different variable within the json structure, like so: { "some_variab ...

Steps for executing Mocha tests in a specified sequence

Background Currently, I am developing a Node.js program where I am creating test suites in Mocha using Chai and SinonJS. The program involves a core graphics module that manages access to a node-webgl context. Due to the nature of node-webgl, I want to i ...