Concealing a button once the collapse feature is toggled in Bootstrap

The following code snippet from the bootstrap website demonstrates how to use collapse functionality:

<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
  Link with href
</a> 
<div class="collapse" id="collapseExample">
  <div class="well">
    ...
  </div>
</div>

I am looking for a way to hide the link that triggers the collapse feature once it is opened. Is there a method to achieve this without needing extra JavaScript?

Answer №1

I used the following method to make the button disappear after it was clicked:

For HTML, I simply added a new class called "hide-me":

<button class="btn btn-default hide-me" data-toggle="collapse" data-target="#search" id="search-display">My Button</button>

Here is the CSS code I used:

.hide-me[aria-expanded="true"] {display: none;}

Answer №2

This solution met my needs perfectly, utilizing Bootstrap without the need for custom CSS or JavaScript:

<a class="btn btn-primary hook in" data-toggle="collapse" href=".hook" aria-expanded="false" aria-controls="collapseExample">
  Link with href
</a> 
<div class="collapse hook">
  <div class="well">
    ...
  </div>
</div>

Check out the fiddle for a live demo:

http://jsfiddle.net/hptrpaxh/1/

Answer №3

Here's a suggestion:

$('.btn').toggle();

You might want to consider adding a unique class or id to this button for better identification.

Answer №4

Apologies for not realizing you were attempting to achieve this without additional JavaScript. I have a simple CSS hack for you. Feel free to customize it according to your needs.

Assign the button a class name like hidden-button Then apply the following CSS styles

.hidden-button {
    position:relative;
    z-index:0;
}


.well {
    position:relative;
    margin-top:-33px;
    z-index:10000;
}

Check out this demo on http://jsfiddle.net/cp5Lvtdo/4/

Answer №5

If you're working with Bootstrap 5 (and possibly version 4 as well), you can achieve this using the built-in Accordion feature. Simply place the button and content in separate accordion item elements, and configure the parent for collapsible content according to the documentation.

I've also made sure to hide the borders of the accordion items by adding b-0.

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ceee3e3f8fff8feedfcccb9a2bda2bf">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="accordion m-4" id="accordionArea">
  <div class="accordion-item border-0">
    <div class="accordion-collapse collapse show">
      <button class="btn btn-primary" role="button" 
        data-bs-toggle="collapse" data-bs-target="#content" 
        aria-expanded="false" aria-controls="content">Toggle button & content
      </button>
    </div>
  </div>

  <div class="accordion-item border-0">
    <div id="content" class="accordion-collapse collapse" 
      data-bs-parent="#accordionArea">
      <div>Some content.</div>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41232e2e35323533203101746f706f72">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

Answer №6

If you want to create a collapsible content section that can be toggled without needing to reload the page, you can wrap the entire content within an <a> element.

Start by placing some link content before the collapsible section and give it a specific class or id. Then, add a custom CSS class to the <a> tag following the instructions outlined in the previous answer, but include a child selector like this:

.toggle-hide[aria-expanded="true"] > /* your link content class or id here */ {
  display: none;
}

By doing this, the link will be slightly less visible, but you'll have the ability to click on the content itself to collapse it and reveal only the link once again.

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

Combining Tailwind with Color Schemes for Stylish Text and Text Shadow Effects

tl;dr I have a utility class in my tailwind.config.ts for customizing text shadows' dimensions and colors. However, when using Tailwind Merge, I face conflicts between text-shadow-{size/color} and text-{color}. The Issue In CSS, text shadows are oft ...

The Axios-generated string I created is returning a 403 forbidden error due to a broken URL, however, it works perfectly fine

When Axios creates a string that combines multiple values to form a URL, it sometimes returns a 403 forbidden error due to a broken URL. For example, the following code works fine: const inventory = await axios.get("http://steamcommunity.com/inventory/765 ...

While continuing to input text, make sure to highlight a specific element from the search results list

Currently, I am using a customized search feature in my React.js application. I am looking for a way to focus on an element within the search results so that I can navigate using arrow keys. At the same time, I need to keep the input field focused to conti ...

Please provide the date using the Foundation Datepicker tool

Beginner in JavaScript here! I am having an issue with submitting dates selected using the Foundation Datepicker from . I have searched for solutions on StackOverflow like Post form on select with JQuery Datepick, but none seem to work in my case. If a Ja ...

Creating a class in JavaScript following an AJAX request

After receiving an AJAX response, the class "navtex-message" doesn't work properly when I create HTML with JavaScript. function formatNtxMessage( m ) { return '<div class="row">' + '<div class="col-lg-9">' ...

Ways to ensure your page stays confidential

Concept: Exploring the idea of making specific pages on a website private. For example, having 4 navigation links where one link leads to a private page that requires a username and password for access. Currently, my practice website has 7 links directing ...

Positioning Problems with Popups

I have been facing an issue with the positioning of a popup in my trivia game. Despite trying multiple solutions, I have not been able to achieve consistency. Here is a snippet of the HTML code: <div class="logo"> <img src="css/rio-40.png"/& ...

The combination of Material UI custom TextField and Yup does not seem to be functioning properly

I am facing an issue with integrating my custom TextField into my RegisterForm along with Yup validation. Whenever I use the custom TextField, I encounter a message "⚠ Champ obligatoire" after clicking on Submit, which is not the case when using a simple ...

Enhanced jQuery Pop-Up Panel

A few issues to address: 1) My goal is to optimize the efficiency of this script. 2) When a user clicks on either pop-out button, it opens a window and hides the element. (Currently using .detach() to remove the embedded video player because in Firefox . ...

Tips for refraining from transmitting visuals to cell phones in a more meaningful way

Typically when working on responsive or mobile-first design, media queries are utilized to deliver different CSS styles based on screen size. An optimal design strategy may involve not including any images in the default (small) resolution layout. While ...

VueJS is unable to identify the variable enclosed within the curly braces

I recently started learning VueJS and I've hit a roadblock. My goal is to display a variable, but for some reason, instead of seeing the product name, all I get is {{product}} Here's the HTML code I'm using: <!DOCTYPE html> <html l ...

Issue with Vue.js 2: Image tag disappears following re-render when fetching data from the server

Working with vuejs in my project was going fine until I encountered this issue: on my page, I have a data grid with an image carousel called "Owl carousel" and the code looks like this: fetchData: async function () { let self = this let query = se ...

css side by side with immovable element

Seeking assistance with CSS. I am looking to create a layout as follows: I want a center-fixed menu with a width of 960px - this part is straightforward. Alongside the menu, I aim to have two divs: one spanning from the left edge of the screen to the cl ...

Comparing a stored array in Mongo against a native JavaScript array with identical length and values results in a failed deep assert comparison

In my mongoose ORM, I have a field in mongo defined as: state: {type: [Number], required: true } When I check a sample document in the mongo console, the state appears as: state: [ 1, 1, 1 ] Everything seems to be in order so far. However, when I try t ...

Having trouble retrieving accurate text information from a JavaScript link

I am encountering an issue with my code which consists of links with divs inside them. When I click on a link, it should display another div with data for "Case No" and "Type". However, the problem is that it only fetches the data from the first link click ...

Reset the form upon submission in AngularJS

Hey, I'm looking to reset the form values after a successful submission. How can I achieve this? <div ng-controller="employeelistController as listControl"> <div class="container form-group" ng-controller="addEmployee as addemp"> ...

Changing the border color in a CSS pseudo-class does not take effect upon clicking

Is it possible to change the border color of an input field when clicked using CSS? I attempted to use the pseudo-class focus for this purpose, but encountered some issues. It seems to only work on select elements and not on elements like text inputs. . ...

Automatically refresh HTML table updates

I'm working with an HTML table and I want to use jQuery to customize it in the following way: If I input a future date, I want jQuery to change the date's color to green. For past dates, I want jQuery to change the date's color to red. Ca ...

Glass-pane or angular/HTML overlay on a designated element

Is there a way to create a glass-pane effect, like an hourglass symbol on a semi-transparent layer, within the boundaries of an HTML element E? The goal is to have the glass-pane only overlap the area within E's boundaries. When the glass-pane is ac ...

A linear gradient effect originating from the center/middle of the background

Is it possible to create a linear-gradient background that starts from the center or middle of the screen? This is the CSS I am currently using: body { display: table; width: 100%; height: 100%; margin: 0 auto; background-repeat: no-r ...