Issue with Second Accordion Panel not refreshing

Within my accordion setup, the initial panel showcases a selection of icons. Upon choosing one of these icons, it triggers the display of one of three panels in place of the second accordion panel, based on the selected icon. The issue seems to be rooted in the JavaScript implementation and the organization within the div.panel class.

HTML

<button class="accordion">
                    <h2 class="float-left">1.</h2>
                    <h2 class="text-center">Screen Type</h2></button> 
                <div class="panel text-center" id="type_panel">
                  <!-- Icon selection labels here -->
                </div>
                <form action="" id="tallyForm" onsubmit="return false;">
                    <button class="accordion">
                        <h2 class="float-left">2.</h2>
                        <h2 class="text-center">Model</h2></button>
                    <div class="panel" id="default_panel">
                      <!-- Model selection options here -->
                    </div>
                    <div class="panel invisible" id="laptop_panel">
                      <!-- Laptop model choices here -->
                    </div>
                    <div class="panel invisible" id="tablet_panel">
                      <!-- Tablet model choices here -->
                    </div>
                    <div class="panel invisible" id="phone_panel">
                      <!-- Phone model options here -->
                    </div>

CSS

JS

JSFIDDLE

Link to JSFiddle

Answer №1

You should define max-height on the element with the class .showing because its parent element div.panel has a max-height: 0 property.

var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  };
}
$("#laptop_button").click(function() {
  $('#default_panel').addClass('invisible');
  $('#laptop_panel').removeClass('invisible');
  $('#laptop_panel').addClass('showing');
  $('#tablet_panel').addClass('invisible');
  $('#phone_panel').addClass('invisible');
});
$("#tablet_button").click(function() {
  $('#default_panel').addClass('invisible');
  $("#tablet_panel").removeClass('invisible');
  $('#tablet_panel').addClass('showing');
  $('#laptop_panel').addClass('invisible');
  $('#phone_panel').addClass('invisible');
});
$("#phone_button").click(function() {
  $('#default_panel').addClass('invisible');
  $("#phone_panel").removeClass('invisible');
  $('#phone_panel').addClass('showing');
  $('#laptop_panel').addClass('invisible');
  $('#tablet_panel').addClass('invisible');
});
.icon-select {
  margin-right: 20px;
  margin-left: 20px;
}

#col1 {
  float: left;
  width: 33%;
  height: 100%;
}

#col2 {
  float: center;
  height: 100%;
  width: 33%;
  overflow: hidden;
  display: table-cell;
}

#col3 {
  float: right;
  height: 100%;
  width: 34%;
  margin-left: 20px;
  overflow: hidden;
  display: table-cell;
}

button.accordion {
  background-color: #ffffff;
  color: #444;
  cursor: pointer;
  padding: 2px;
  width: 50%;
  text-align: left;
  outline: none;
  transition: 0.4s;
  border-left: 1px solid #6fdd7a;
  border-right: 1px solid #6fdd7a;
  border-top: 1px solid #6fdd7a;
  border-radius: 4px;
  border-bottom: none;
}

button.accordion.active,
button.accordion:hover {
  background-color: #6fdd7a;
  color: #ffffff;
}

div.panel {
  padding: 0px 18px;
  background-color: #ffffff;
  max-height: 0;
  overflow: hidden;
  width: 46%;
  border-right: 1px dotted #6fdd7a;
  border-left: 1px dotted #6fdd7a;
  transition: max-height 0.2s ease-out;
}

#optional_panel {
  border-bottom: 1px solid #6fdd7a;
}

label > input {
  visibility: hidden;
  position: absolute;
}

label > input + img {
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 2px;
  -webkit-transition: all 0.25s linear;
}

label > input:checked + img {
  background-color: #6fdd7a;
}

.invisible {
  display: none;
}

div.showing {
  padding: 0px 18px;
  background-color: #ffffff;
  max-height: 600px;
  overflow: hidden;
  width: 46%;
  max-height: 100%;
  border-right: 1px dotted #6fdd7a;
  border-left: 1px dotted #6fdd7a;
  transition: max-height 0.2s ease-out;
}

// JUST STYLES THE RADIO BUTTONS AND 
.control {
  font-size: 18px;
  position: relative;
  display: block;
  margin-bottom: 15px;
  padding-left: 30px;
  cursor: pointer;
}

.control input {
  position: absolute;
  z-index: -1;
  opacity: 0;
}

.control__indicator {
  position: absolut...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="accordion">
  <h2 class="float-left">1.</h2>
  <h2 class="text-center">Screen Type</h2></button>
<div class="panel text-center" id="type_panel">
  <label class="icon-select">
    <input type="radio" name="type" id="laptop_button" /> <img src="icons/iconmonstr-laptop-4-120.png" alt="laptop"> </label>
  <label class="icon-select">
    <input type="radio" name="type" id="tablet_button" /> <img src="icons/iconmonstr-tablet-1-120.png" alt="tablet"> </label>
  <label class="icon-select">
    <input type="radio" name="type" id="phone_button" /> <img src="icons/iconmonstr-smartphone-3-120.png" alt="phone"> </label>
</div>
<form action="" id="tallyForm" onsubmit="return false;">
  <button class="accordion">
    <h2 class="float-left">2.</h2>
    <h2 class="text-center">Model</h2></button>
  <div class="panel" id="default_panel">
    <label>
      <h3 class="text-center">Please select a Device Type above</h3></label>
  </div>
  <div class="panel invisible" id="laptop_panel">
    <div id="col1">
      <label class="control control--radio">LAPTOP
        <input type="radio" name="radio-model" value="laptop1" />
        <div class="control__indicator"></div>
      </label>
    </div>
    ...
  </div>
  <div class="panel invisible" id="tablet_panel">
    <div id="col1">
      <label class="control control--radio">iPad 2
        <input type="radio" name="radio-model" value="tablet-ipad2" onclick="calculateTotal()" />
        <div class="control__indicator"></div>
      </label>
      ...
  </div>
  <div class="panel invisible" id="phone_panel">
    <div id="col1">
      <label class="control control--radio">iPhone 3 &amp; 4
        <input type="radio" name="radio-model" value="phone-iphone3" />
        <div class="control__indicator"></div>
      </label>
      ...
  </div>

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

Effective strategies for integrating Bootstrap and AngularJS without relying on jQuery

Currently exploring AngularJS after experimenting with Twitter's Bootstrap. I appreciate Bootstrap for its simplicity, sleek design, and mobile-friendliness. On the other hand, I have noticed a trend where people recommend using AngularJS over jQuery, ...

Having trouble converting an Amazon S3 function into promises with when/node

I'm having trouble with an AWS S3 asynchronous function and encountering a strange issue. Here is the code snippet: var s3 = new AWS.S3(); var when = require('when'); var nodefn = require('when/node'); var getObjectP = nodefn.lif ...

JavaScript - An unexpected error occurred: Syntax error, unrecognized expression: [href=#contact] (WordPress)

I am currently working on a feature that involves adding a class to a specific menu item when a certain section is in view. However, I encountered an error that reads: Uncaught Error: Syntax error, unrecognised expression: [href=#contact] Your help would ...

When attempting to execute Protractor tests, an error occurs stating that the object #<Object> does not possess the 'getInstance' method

Whenever I try to run my Protractor tests from the command line, all of them fail because the protractor object does not have the necessary methods. The error message I receive is: TypeError: Object # has no method 'getInstance' Although this ...

Display all attribute connections for a product in Prestashop

I have updated the products in my shop to include different combinations. These combinations involve a 'packaging' attribute with two options: a 100 units box and a 200 units box, each with its own unique reference number. On the product page, t ...

What is the best way to ensure a textfield remains aligned with its label in ExtJS?

Hey there, I am working on a large window that contains a form with multiple text fields. The default alignment has the labels to the left and the input boxes centered. Is there a way to keep the input boxes aligned next to their respective labels on the ...

Is it possible to employ a method to eliminate a specific valuable element 'this' from an array?

I am working on a task management app that allows users to create a To-Do list and remove specific items from the list. Currently, I am facing an issue with using 'localStorage' to save the list when the page is refreshed. The problem arises when ...

Can CSS be used to create curved dashed lines?

Is it possible to achieve dashed lines similar to the ones highlighted in the images below using only CSS? I have a responsive web page built with Bootstrap, and I need the dashed lines to adjust accordingly when the window width changes. https://i.sstat ...

How can attributes be passed to a Vue JS computed property getter and setter?

Attempting to utilize computed properties for binding input fields with the Vuex Store has presented some challenges. Each Data Stream in my application has dynamic input fields, managed through the admin section. I need to allow users to select a specific ...

Is JavaScript necessary for this task in PHP?

Hi everyone, I recently discovered a way to style a PHP echo in a more visually appealing manner. Instead of presenting an unattractive colored box right away, I was wondering if there is a way to only display the box when the user selects the calculation ...

I am encountering an issue where propData in Vue Jest is returning as undefined

I have encountered an issue while passing the propData in my jest test file for a Vue component. It seems that the propData is not being set to the component and instead, I am receiving an error saying "cannot read property of clouds of undefined." Could i ...

Obtain the URL value using jQuery and place it inside a <span> element

How can I insert a href value into the span tag like this? <p class="name"> <a download="adja-lo.pdf" title="adja-lo.pdf" href="http://localhost/MatrixDRSnews/apps/Matrix/server/php/files/adja-lo.pdf">adja-lo.pdf</a> </p> ...

Personalizing the Twitter Embedded Timeline

I am curious about how much customization options are available for the Embedded Timeline. I searched through the documentation but still have some unanswered questions - maybe I missed something. My goal is to initially display only one tweet and then ha ...

Starting your React application with the `npm start` command

After creating my react app using the create-react-app command, I named it react-app. These were the steps I followed: Navigate to the directory by typing cd react-app/ Run the command npm start Encountered an error message that reads; npm ERR! Missing ...

altering dimensions in three.js

My question may seem a bit silly, but it's about resolution in THREE.js. I have experience with OpenGL and some frameworks related to it, so naturally, I became interested in webGL and Three.js. After trying out some simple demos, I decided to create ...

Locating marker coordinates on the screen with AR.js

After implementing the code below, I successfully retrieved the marker's position: this.marker.object3D.getWorldPosition(vector); Now, I am eager to understand if there is a way to convert this position (x,y,z) into its corresponding screen coordinat ...

The object in question does not have the capability to support the "live" property or method

I've encountered an issue in IE related to a script error with the live method. The problem arises when testing on the website URL @ Despite trying multiple solutions and various fixes, I haven't been able to resolve the issue yet. Any assistanc ...

Show feedback on the webpage

I've encountered a challenge when trying to post comments on the page based on the posting ID. Controller: public function viewUserQuestion(Post $post) { $comment = Comment::where('post_id', $post->id)->get(); return view(& ...

jQuery Ajax fails to serialize the input type file field

Here is the Razor code snippet that I use to return a form to a jQuery function upon submission: @model Slider @{ Layout = null; } @using (Html.BeginForm("AddOrEdit", "Slider", FormMethod.Post, new { enctype = "multipart/form-data" ...

How to maximize efficiency by utilizing a single function to handle multiple properties in Angular

I have 2 distinct variables: $scope.totalPendingDisplayed = 15; $scope.totalResolvedDisplayed = 15; Each of these variables is connected to different elements using ng-repeat (used for limitTo) When the "Load More" button is clicked (ng-click="loadMore( ...