Skip to content
Advertisement

Add class to single div per click

I’m new in programming.

I have multiple sibling divs with the same class and a view more button. I want to add class to the div on the view more button click, but one by one. So the first sibling-div will be visible and view more button. When a user clicks on the button the second sibling-div should be visible, then on clicking again the third sibling-div should be visible and so on.

I don’t have access to html so i used jQuery to add the view more button.

I have used CSS to hide all the sibling divs by default. Only the first div, view-more-btn button and when there’s active class they will be visible.

I’ve tried to add class using jQuery but it adds to all the divs.

This is the html

JavaScript

This is my CSS

JavaScript

This is my jQuery

JavaScript

I’ve tried all this methods but it doesn’t work the way I want. It adds class to all the sibling-div, or just the second sibling-div

Advertisement

Answer

You can access the next hidden element in the group with

JavaScript

:hidden targets elements with no opacity or display:none. :first is a jQuery selector that targets the first item of the match.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement