Restricting the amount of visible

I have a Q&A section with multiple stacked boxes. I noticed that opening all the boxes made the page too long, so I'm exploring if JavaScript or jQuery can help limit the number of active boxes.

$(document).ready(function() {
  $(".faqOuter").click(function() {
    $(this).toggleClass('faqOuter-change');
    $(this).parent().find('.faqInner').slideToggle(500, 'swing');

    if ($(this).parent().height() == 75) {
      $(this).parent().animate({
        height: '225'
      }), 500, 'swing';
    } else {
      $(this).parent().animate({
        height: '75'
      }), 600, 'swing';
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="faqSection">
  <div class="faqSectionSub">
    <h4 class="faqOuter">Trial One Bar</h4>
    <div class="faqInner">
      <p>Alienum phaedrum torquatos nec eu, vis detraxit periculis ex, nihil expetendis in mei.</p>
    </div>
  </div>
  <div class="faqSectionSub">
    <h4 class="faqOuter">Trial One Bar</h4>
    <div class="faqInner">
      <p>Alienum phaedrum torquatos nec eu, vis detraxit periculis ex, nihil expetendis in mei.</p>
    </div>
  </div>
  <div class="faqSectionSub">
    <h4 class="faqOuter">Trial One Bar</h4>
    <div class="faqInner">
      <p>Alienum phaedrum torquatos nec eu, vis detraxit periculis ex, nihil expetendis in mei.</p>
    </div>
  </div>
  <script src="scripts.js"></script>

https://i.sstatic.net/EzbGn.jpg

How can I make sure only one box is displayed at a time?

Answer №1

You should definitely consider exploring the Accordion feature from JqueryUI.

For more information, check out this link.

If you want collapsible content, I recommend checking out this option.

Answer №2

If you want to have inner divs open on click using JavaScript, just add the following line of code to hide all inner boxes by collapsing or hiding them completely.

  $(".faqInner").css("display","none");

Make sure your document.ready function looks like this:

$(document).ready(function() {
  $(".faqOuter").click(function() {
    $(this).toggleClass('faqOuter-change');
    $(this).parent().find('.faqInner').slideToggle(500, 'swing');

    if ($(this).parent().height() == 75) {
      $(this).parent().animate({
        height: '225'
      }), 500, 'swing';
    } else {
      $(this).parent().animate({
        height: '75'
      }), 600, 'swing';
    }
  });
  $(".faqInner").css("display","none");
});

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

Error: 'err' has not been defined

Recently, I embarked on creating my very first API using Mongo, Express and Node. As I attempted to establish an API endpoint for a specific user, the console threw me this error: ReferenceError: err is not defined. Curiously, the same method worked flawle ...

The Ajax request is failing to function properly

Check out this code snippet I have: var _07votes = $("#07votes"); $(document).ready(function() { setInterval(function() { $.ajax({ url: "http://services.runescape.com/m=poll/retro_ajax_result?callback=?", ...

Is it feasible to transmit telemetry through a Web API when utilizing ApplicationInsights-JS from a client with no internet connectivity?

My Angular application is running on clients without internet access. As a result, no telemetry is being sent to Azure. :( I am wondering if there is a way to configure ApplicationInsights-JS to call my .Net Core WebApi, which can then forward the inform ...

What is the best way to programmatically change the event tied to an element's click

<div id="filters"></div> <div id="available"> <span class="badge badge-pill" onclick="addFilter()">Label</span> </div> function addFilter(event) { document.getElementById("filters").appendChild(eve ...

Circular Graphical Representation using CSS3 styling

Check out my latest creation - a donut chart: http://jsfiddle.net/4azpfk3r/217/ I'm trying to customize the appearance of the donut chart. Is there a way to give it a red outline and have the filled score/percentage in solid red, while leaving a tran ...

Using ng-model within ng-repeat for data binding in AngularJS

My challenge is to connect a model called 'User' to a series of input fields. Since I don't know the specific fields in advance, I need to write generic code that dynamically sets up the form based on the available fields. <script> ...

Axios encounters difficulty retrieving the response data following a POST request

When using axios to post data and fetch a response, I am encountering an issue where the data is successfully posted but the response data cannot be printed. This works correctly in Postman, so I'm not sure if the problem lies with the backend or fron ...

Unable to convert the value "undefined" to an ObjectId (type string) in the "_id" path for the "Order" model

As someone who is new to Javascript and working on an e-commerce website, I am currently facing a challenge with displaying the order id on the return page. Each time I try to do so, I encounter an error message that reads Cast to ObjectId failed for val ...

Div container placed outside of its parent div

I am struggling to understand why my project-img-text-container is protruding outside its parent div project-image-container and project-img-main. I tried adding project-image-container as a solution, but it didn't work. Both containers are set to rel ...

Dynamic scrolling feature for lists that moves horizontally

When working with a lengthy list, I encountered an issue with horizontal scrolling. It worked fine when the list was statically implemented as shown below. <div style="margin-top:100px;white-space: nowrap;"> <ul > <li style= ...

5 Bootstrap Popovers with Custom HTML Contents

I am having trouble separating the content of the bootstrap5 popover from the HTML attributes, unlike other components where it is achievable. var popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]')) var ...

Would it be beneficial to create classes for frequently used CSS styles and apply them to HTML elements?

Check out my code snippet: style.css .bg-cover{background-size:cover; background-position:center;} .opacity-1{opacity:0.1;} .border-3-solid{border-width:3px; border-color: solid;} .black-border{border-color:#000;} .full-width{width:100%;} index.html ...

What could be the reason for v-model not functioning properly?

Embarking on my Vue.js coding journey, I am currently following a straightforward online tutorial. Utilizing the vue-cli, I kickstarted my application and crafted a basic component named Test.vue. Within this component lies a simplistic input control conne ...

A mysterious JavaScript snippet that automatically scrolls down when the webpage is loaded

Recently, I encountered an issue with a mysterious JavaScript script that automatically scrolls down when entering the page (I suspect it's JavaScript). I've tried to investigate using Firebug, examining the code, deleting scripts, and even remov ...

Angular 8 allows for dynamic updates as users provide input and make selections with check marks

When using Angular 8, if a user inputs "$10" and then clicks on it (using the OnClick event), the box below should be updated with the user input and the entire box should be selected like the example shown in the picture. I would greatly appreciate any t ...

What is the best way to integrate TimeOut feature into an existing slider code?

I'm currently working on a simple slider with buttons that is functioning well. However, I am looking to incorporate the TimeOut() function into the existing code to enable automatic slide transitions. My attempts to achieve this using jQuery have be ...

Incorporate a Vue component into a string for seamless integration with Buefy's confirmation feature

Can a vue component be loaded into a string for use with buefy confirm? I attempted the following approach, but it did not work as expected: archiveChannelPrompt () { const archiveChannel = document.createElement('template'); new Vue({ ...

Retrieve information from MariaDB using nodejs and transmit it to an html document (Express JS)

To create a calendar with activities, I am retrieving data from a local Maria database and populating an HTML table. My approach involves using Express JS to direct users to a designated HTML page. Here is how my file structure looks like: NODE | |- app ...

Unusual marker appearing on every page utilizing ionic v1 and angularjs

For some unknown reason, a dot appears at the upper left side of each page in my webapp: https://i.stack.imgur.com/nN61t.png It seems to be an issue with the HTML code. When I run the snippet below, the dot is visible: <ion-view view-title="Send fe ...

Guide for using JavaScript to obtain the current line position within a contenteditable div during a keypress event

I am in the process of developing a customized editor using a contenteditable div. I require a javascript code that can determine the line position of the current caret position during a keypress event. It should be able to adapt when new lines are added o ...