jQuery siblings toggling issue unresolved

I am new to jQuery and I'm trying to create a toggle effect between sibling divs. In other words, when I click to show the next div, the previous one should automatically close. Currently, the previous div doesn't close when the next one is opened, and I can't figure out why. I have to manually open and close each div.

Apologies for the beginner question! :)

Any assistance would be greatly appreciated!

function showHide(button){   
$(button).next('.showhide').toggle().siblings('.showhide').hide();
} 

The HTML:

<div class="acc-s-container">
   <div class="acc-s-header" id="div" onclick="showHide(this)">
      <h1>Account Settings</h1>
   </div>
   <div id="divcontent" class="acc-s-content showhide">
      <p>content</p>
   </div>
</div>
<div class="acc-s-container">
   <div class="acc-s-header" id="div2" onclick="showHide(this)">
      <h1>Account Settings</h1>
   </div>
   <div id="divcontent2" class="acc-s-content showhide">
      <p>content</p>
   </div>
</div>
<div class="acc-s-container">
   <div class="acc-s-header" id="div3" onclick="showHide(this)">
      <h1>Account Settings</h1>
   </div>
   <div id="divcontent3" class="acc-s-content showhide">
      <p>content</p>
   </div>
</div>

CSS

 .acc-s-content{display:none;}

Answer №1

The selector you are using to target elements with the class .showhide from sibling .acc-s-container divs of the clicked div's parent is incorrect. Try this instead:

function toggleShowHide(button){  
 $(button).next('.showhide').toggle().parent().siblings().find('.showhide').hide();
} 

View Working Demo

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 div remains stationary on the initial click, but smoothly slides down right after

My portfolio features a set of thumbnails showcasing my work. When you click on an image, the row of thumbnails slides down to unveil a slideshow and relevant information at the top. The issue I'm facing is that, upon the first click, the information ...

Having trouble with your custom accordion content in React JS not sliding open?

Check out my progress so far with the fully functioning view here This is the structure of the Accordion component: const Accordion = ({ data }) => { return ( <div className={"wrapper"}> <ul className={"accordionList ...

What happens to the arrays within my function?

I am working on a controller code: public function getChartData(){ $result["categories"] = array(); $result["series"] = array(); $sales = $this->db->Execute("select id_tenant, nama_kantin, count(id_penjualan) as jumlah_penjualan, s ...

Display progress bar dialogue upon submission

I am looking to implement a jQuery popup that displays "Loading..." on every postback. I am unsure of how to achieve this and which technologies to use. My goal is to create the code in a superclass so that it only needs to be coded once. Ideally, when I c ...

Error message: An attempt to call an undefined function get_query_var() has caused a fatal

Recently I encountered a file named: taxonomy-product_cat-ford.php On my homepage, there is a menu that displays: All car | ford | toyota By default, the homepage always shows all cars. To specifically load the content of taxonomy-product_cat-ford.php, I ...

Adjust the autofocus to activate once the select option has been chosen

Is there a way to automatically move the cursor after selecting an option from a form select? <select name="id" class="form-control"> <option>1</option> <option>2</option> <option>3</option&g ...

What is the best way to combine two rows in a data table?

As I progress with my endeavor to construct a user-friendly transition matrix in R, following up on the previous post regarding adding a vertical line to the first column header in a data table, I am faced with a new challenge. Upon executing the provided ...

Is it possible to alter the background behind each word in a text using only CSS?

I have a question about styling quotes by highlighting each word in the text with a background color. The issue I'm facing is that when using a solid background, there are no gaps between the colors. How can I achieve a result similar to this, but wit ...

Top approach for inserting Class instance into a group

I need some guidance on how to approach this issue. I am interested in creating a set of objects similar to the example below: Person P = new Person(); P.Name = 'John'; P.Surname = 'Dough'; var People = []; People.push(P); Can this b ...

retrieving data from Redis with the help of Node.js

I'm having trouble with the code snippet below. Despite setting values for the left_label and right_label variables in Redis, they always seem to return as "true". I suspect this is due to the client.get function returning true when successful. How ca ...

Unable to display the retrieved list data using v-for loop from axios get request

I recently started using Vue.js and decided to create a simple HTML website with Vue.js to display data from myphpadmin using axios. Everything was working smoothly until I encountered an issue where I couldn't display the JSON message on my screen. n ...

Having trouble implementing responsive image with fixed aspect ratio code

I've tried several tutorials online to make this image responsive with a fixed aspect ratio, but nothing seems to work. The image is set at 900x400px and I want it to remain fully visible on smaller screens as well. Here's the codepen link < ...

What is causing my two divs to fail in expanding to the full height of the parent div?

HTML: <div id="main" class="rounded-corners"> <div id="benefits"> <img src="/benefits-heading.png" style="padding: 30px;" /> <div id="corporateside"> ...

Exploring the power of Angular 2 Directive scopes

When it comes to Angular2, Directives do not come with "scopes", unlike Components. However, in my specific case, I require a Directive to establish a scope. Take a look at my App component - it contains an HTML template, and the foo directive could potent ...

Successive Node.js Post Requests

Is it possible to make consecutive POST requests in separate route files and redirect to a different EJS page with each request? Here is my file structure: Routes: index.js - users.js - users1.js Views: index.ejs - users.ejs - users1.ejs This is my ...

Tips for stopping a flex:1 div from expanding to accommodate its abundant content

I'm facing a situation where I need to create two columns, one fixed at 150px and the other filling up the remaining space with scroll functionality for overflow content. Despite trying to use flexbox for this layout, the second column keeps expanding ...

Navigating the world of cryptocurrency can be daunting, but with the right tools

Hello fellow developers, I've encountered some difficulties with cryptocurrency in my Nativescript project. I am attempting to incorporate the NPM package ripple-lib into my code, but I have been unsuccessful using nativescript-nodeify. How can I suc ...

Innovative: Enhancing column width dynamically without compromising grid integrity

Currently, I am utilizing Bourbon's Neat library to structure my grid system. Within my code, I have the following setup: section { @include outer-container; aside { @include span-columns(3); } article { @include span-columns(9); } } The chal ...

Error encountered in Flatpickr NextJS+TS application: Uncaught DOMException - accessing CSSStyleSheet.cssRules getter is not permitted for cross-origin stylesheet

After successfully integrating Flatpickr into my project, I encountered an issue when testing in mobile view. When trying to open the calendar by clicking on the input field, I received the following error: Uncaught DOMException: CSSStyleSheet.cssRules get ...

Scoped variable in Typescript producing a generated Javascript file

I'm currently learning TypeScript through an online course, and I've encountered a problem that seems to be related to a VSCode setting. Whenever I compile app.ts, it generates the app.js file, but I immediately encounter a TypeScript error. It& ...