Is Bootstrap the antithesis of collapse?

I am fairly new to working with Bootstrap. While I have a script that functions, I am struggling to make modifications to it.

The script has two main functionalities:

1) Clicking on a header will toggle the visibility of the following div.

2) Clicking on a header will also display one of two glyphicons next to the header depending on whether the following div is being opened or closed.

This functionality resembles what can be seen in Wikipedia's mobile pages (e.g. ). However, unlike Wikipedia's default open settings for JS gizmos, I want visitors to see only a column of headers accompanied by down arrows (chevron-down glyphicons) by default.

While this may seem straightforward, none of my attempts have been successful so far. Below is my HTML code:

<div data-toggle="collapse" data-target="#target">
  <h2>
   <span class="only-collapsed glyphicon glyphicon-chevron-down"></span>
   <span class="only-expanded glyphicon glyphicon-remove-sign"></span>
   Test 1
  </h2>
</div>
<div id="target" class="collapse in">Hello, world!</div>

<div data-toggle="collapse" data-target="#target2">
  <h2>
   <span class="only-collapsed glyphicon glyphicon-chevron-down"></span>
   <span class="only-expanded glyphicon glyphicon-remove-sign"></span>
   Test 2
  </h2>
</div>
<div id="target2" class="collapse in">Hello, world!</div>

And here is my CSS code:

.only-collapsed { display: none; }
.collapsed .only-collapsed { display: inline; }
.collapsed .only-expanded { display: none; }

While the setup currently mirrors Wikipedia's mobile page behavior, I aim to have my text blocks CLOSED by default instead. By making changes to the CSS styles as shown below, I can successfully reverse the direction of the glyphicons. However, this alteration does not affect the visibility of the text content within the divs:

.only-expanded /* Down Arrow */ { display: none; }
.collapsed .only-expanded { display: inline; }
.collapsed .only-collapsed { display: none; }

In essence, though the headers initially show a down arrow (as desired), the text is still visible when it should remain hidden.

I assumed that the opposite of collapse could be expand or open, and accordingly tried modifying my HTML and CSS using these terms without success. There must be a simple solution that I might be overlooking; any pointers would be greatly appreciated!

I came across a potentially helpful discussion at Twitter bootstrap collapse: change display of toggle button, but it seems a bit more complex than what I am aiming to achieve, and I haven't fully grasped its implications yet.

Regardless, is there an efficient and straightforward solution that someone could share?

ON EDIT:

Having implemented dim4web's suggestion, there has been improvement in the sense that the default view now displays a down arrow without showing the text content. However, the issue lies in that when the text is expanded upon clicking the header, the down arrow remains visible instead of being replaced by an 'X' as intended.

Here is my updated HTML code:

<div data-toggle="collapse" data-target="#target">
  <h2>
   <span class="only-collapsed glyphicon glyphicon-chevron-down"></span>
   <span class="only-expanded glyphicon glyphicon-remove-sign"></span>
  New Test
  </h2>
</div>
<div id="target" class="collapse">Hello, Universe!</div>

<div data-toggle="collapse" data-target="#target2">
  <h2>
   <span class="only-collapsed glyphicon glyphicon-chevron-down"></span>
   <span class="only-expanded glyphicon glyphicon-remove-sign"></span>
  New Test
  </h2>
</div>
<div id="target2" class="collapse">Hello, Universe 2!</div>

Answer №1

The reverse of collapsed is:

class="panel-collapse  collapsed collapse show"

Answer №2

http://jsfiddle.net/21fsx8zc/

  1. utilize .collapse('hide') to conceal/close all div.collapse
  2. implement show.bs.collapse and hide.bs.collapse to display or conceal appropriate icons as required

CSS:

.only-expanded {
    display:none
}

JQ:

$('.collapse').collapse('hide').on('show.bs.collapse', function (e) {
    var id = $(this).attr('id');
    $('div[data-target="#' + id + '"]').find('.only-expanded').show();
    $('div[data-target="#' + id + '"]').find('.only-collapsed').hide();

}).on('hide.bs.collapse', function () {
    var id = $(this).attr('id');
    $('div[data-target="#' + id + '"]').find('.only-expanded').hide();
    $('div[data-target="#' + id + '"]').find('.only-collapsed').show();
})

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

Issue with displaying random 1px background image on Chrome browser

I'm facing an issue with the background image not appearing for one of the links in my navbar in Chrome. Specifically, the image doesn't show up between the "Athletics" and "Bowling" links. Interestingly, the background image displays fine in Fi ...

Steps to position an image without a background in the middle of a smaller container:

My HTML includes the following code: <div style="width:400px;height:300px;overflow:hidden;"> <img src="http://d39kbiy71leyho.cloudfront.net/wp-content/uploads/2016/05/09170020/cats-politics-TN.jpg" /> </div> Take a ...

Layering an object on top of a clone using CSS

I am working on a webpage with a jQuery menu located at the following link: Menu To accommodate more items and have greater control, I duplicated the menu twice. However, when hovering over them, the duplication is visible in the background. Is there a ...

What is the best way to trigger a parent function from a Bootstrap modal within a Vue.js component

In my vue.js component, I am calling a Bootstrap modal within a function. I want to call another function after the modal is closed, but I am facing issues with the scope. this.myFunction() // This works here $('#app-modal-warning').on('hi ...

Issues with data loading from server persist with JSON in JQuery Datatables on IE7, IE8, and IE9

I have successfully implemented Jquery Datatables on a client's admin panel. After testing it on Chrome, Firefox, and Opera, everything seemed to be working perfectly. However, when I attempted to load it on IE7, IE8, or IE9, the system just stuck on ...

Fixing the Jquery animation glitch triggered by mouseover and mouseout

In my project, I have implemented a small mouseover and mouseout functionality. The challenge I am facing is that I need to keep the mouseout function using animate() instead of css() for specific reasons. The issue arises when I quickly do a mouseover fo ...

Implement a JavaScript and jQuery code that alternates between fading in and fading out every two items in a

Can someone help me figure out how to create a loop that fades in and out every 2 items from an unordered list using jQuery? I've been trying to do this, but my loop only processes one item at a time. <div> <ul> <li>item1</li ...

What are some techniques for styling a field when the div id is not specified?

I need to customize a data field within a table, but I am unable to locate or identify its div ID. Here is the page source: <tbody> <tr> <td style="font-size:12px; text-align:center;" name=""> <div sty ...

Internet Explorer failing to trigger Ajax requests

I'm encountering an issue with my script on my webpage - it works perfectly in Chrome, but Internet Explorer is not entering the success{} function of Ajax. It does go into the Complete{} function without any problems. I attempted to pass the data var ...

Tips for Uploading the Contents from 2 Select Boxes

In my ASP.NET MVC application, I have two list boxes named #AvailableItems and #AssignedItems. I am able to transfer items from one box to the other. Additionally, there are related values stored as attributes on the Save link that also need to be submitt ...

The paragraph fails to trigger the CSS :hover effect when I assign it an ID

Here is an example of a CSS3 transition with ease effect: HTML <div class="button"> <a href="#" onMouseOver="clicksound.playclip()"></a> <p id="myId" class="top"></p> </div> CSS * { pad ...

Elastic video player embedded in a flexbox

I'm having an issue trying to make a responsive YouTube embed work inside a flex container alongside other content. The problem is that the container for the YouTube embed doesn't resize properly and ends up overlapping with other elements. To be ...

Tips for effectively utilizing the mock-ajax library in conjunction with Jasmine

Currently, I have a function that requests JSON data and then modifies the DOM in the success function. To simplify my Jasmine tests, I am attempting to utilize the mock-ajax library instead of exposing private functions for mocking. Despite setting reque ...

Having some trouble with the JS code, so I decided to switch gears and focus on demonstrating some CSS concepts in

After spending around 3-4 hours trying to fix a demo using JS and CSS, I finally found half of the solution. Despite adding the CSS directly to the element which worked fine, I encountered issues when attempting to use JS. I downloaded the jquery-1.11.3.j ...

What's causing this bootstrap button to act up?

https://i.sstatic.net/9ZAz9.png It was expected that the button would appear on the right side while the navigation titles would be displayed on the left. <nav id="mainNavbar" class="navbar navbar-dark bg-dark navbar-expand-md"> ...

Follow these steps to position a child div absolutely within a grandparent div with the following structure

Is it possible to make the child div absolute for the grand parent div without changing the parent div's position? Here is the scenario: the parent div is a relative div, but the grand parent div is also a relative div. How can we achieve this without ...

Utilizing Ajax to fetch a div element from a web page

Hey there! I have a link set up that loads a page into a specific div ID, which is #ey_4col3. The issue I'm facing is that it loads the entire page along with all its contents, but what I really want to load from that page is just the content within ...

The motion of a Wordpress website varies

Whenever I click on the logo of my website, an issue arises where the entire page shifts to the right side and then returns back to its original position. It only requires one or two clicks for this problem to occur, but it is definitely present. Thank yo ...

Utilizing PHP for loading a fresh webpage within a specific div element of an existing page through AJAX

<!doctype html> <head> <title>Homepage</title> <style> #header{background-color:#09C; width:100%; height:100px;} #logoandsearch{background-color:#603; width:400px; height:40px; float:left; margin-top:30px; margin-left:1 ...

Preventing bootstrap.html or index.html from being included in the history stack using jQuery mobile

I'm currently developing a mobile application using jQuery mobile and PhoneGap. Upon launching the app, a dynamic frontpage is loaded based on certain conditions, such as whether the app has been configured or not. To handle this, I have created a b ...