Updating Bootstrap accordion button title when in collapsed state using CSS

I need some assistance with updating a Bootstrap accordion button behavior. Specifically, I am trying to change it so that when collapsed, it displays the text "See Less -". I believe I should be using the CSS property content, but I'm struggling to pinpoint exactly where to make this adjustment within my HTML code:

    <h2>Some Heading</h2>
    <div style="display:table; margin:auto;">
       <ul style="margin-bottom: 0">
          <li class="services-list">Item 1</li>
          <li class="services-list">Item 2</li>
          <li class="services-list">Item 3</li>
       </ul>
    </div>
       <div id="collapse-custom" class="collapse">
          <div class="card card-block">
             <div style="display:table; margin:auto; width: 49%;">
                <ul>
                   <li class="services-list">Item 4</li>
                   <li class="services-list">Item 5</li>
                   <li class="services-list">Item 6</li>
                   <li class="services-list">Item 7</li>
                   <li class="services-list">Item 8</li>
                </ul>
             </div>
          </div>
       </div>
<button class="btn btn-primary" type="button" data-toggle="collapse" 
    data-target="#collapse-custom">See More<i class="fa fa-plus" 
    style="padding-left: 10px;"></i>
</button>

Any guidance on how to achieve this modification would be greatly appreciated. Thank you in advance!

Answer №1

Here's an alternative method that is more Bootstrap-friendly:

  1. Start by giving the button a unique ID:

<button id="btn-service-list" class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapse-custom">See More<i class="fa fa-plus" style="padding-left: 10px;"></i>

  1. Add this JavaScript code to your .js file or within a script tag on the same page:

$('#collapse-custom').on('shown.bs.collapse',function(){
    $('#btn-service-list').text('- Show less');
});
$('#collapse-custom').on('hidden.bs.collapse',function(){
    $('#btn-service-list').text('+ Show more');
});

Answer №2

Assign a class to your button, such as btn-toggle:

<button class="btn btn-primary btn-toggle" type="button" data-toggle="collapse" 
    data-target="#custom-collapse">Show More<i class="fa fa-plus" 
    style="padding-left: 10px;"></i>

If you're using jQuery, you can add the following code:

<script type="text/javascript">
        $(function () {
             $(".btn-toggle").click(function () {
                    if($(".btn-toggle").text()=="Show More"){
                        $(".btn-toggle").text("Show Less -");
                    }
                    else {
                        $(".btn-toggle").text("Show More +");
                    }
                });
          });       
</script>

Hopefully, this solution works for you!

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

How can we ensure that the slider thumb label is always shown in Angular 16 Material components?

Is there a way to display the thumb label constantly on a material slider? The solution mentioned above does not seem to work with Angular 16 material UI slider. Could you kindly offer an alternative solution for this issue? ...

Activate the Flexslider when it comes into view

Currently using flexslider, I am attempting to create a smooth transition from the second slide back to the first as soon as the slider is in view. My initial idea was to implement if (scroll >= 500px && scroll < 600px) however, I am uncerta ...

Bootstrap's innovative design for a data grid

I'm currently working on designing a Grid using bootstrap 3. https://i.sstatic.net/DZzcq.png My tools include html5, css, and bootstrap integrated with React.js. The main feature of my project is a data grid (specifically, a Fixed Data Table). ...

How to display text on a new line using HTML and CSS?

How can I properly display formatted text in HTML with a text string from my database? The text should be contained within a <div class="col-xs-12"> and nested inside the div, there should be a <pre> tag. When the text size exceeds the width o ...

Nest a Div inside another Div with a precise margin of 10 pixels

Recently, I attempted to create a fullscreen webpage like this one: https://i.sstatic.net/21KCr.png My goal was to have the color of each element change randomly every second. Here is the JavaScript code I implemented: <script> var tid = se ...

Jquery for controlling the navigation menu

I'm new to JavaScript and facing 3 challenges: 1. When I click on the parent <li>, the correct content of the child <sub> does not show up. Each category like "colors, shapes, sizes" should have corresponding child categories like "green, ...

What is the best way to achieve this Bootstrap design without copying the content?

Trying to achieve a specific Bootstrap layout without repeating content is my current challenge. Essentially, I aim to divide some content into 2 columns at the sm/md breakpoints and then switch to 3 columns for the lg breakpoint. The layout for sm/md bre ...

Why isn't the Mat error functioning properly in this Angular code?

Could someone explain why the Mat error does not seem to be functioning properly in this Angular code snippet? <div class="form-group"> <mat-form-field class="row-styling"> <mat-label for="aplctnName"> Application Name <sp ...

Implementing reCAPTCHA and PHP for Email Form Submission

Currently using GoDaddy as the web host for my website. Struggling to integrate reCAPTCHA into the email form on my site. I successfully pass the reCAPTCHA test, but emails are not being sent. So far, the website has been built using PHP and HTML only. ...

The Bootstrap navigation bar is not functioning as expected

After building a landing page with a bootstrap navbar, I noticed that the hamburger menu doesn't display the list of items when the screen size is decreased. Here is the code I used: <nav class="navbar navbar-expand-lg" id="navbar&qu ...

Problem with JavaScript and Basic HTML5 Canvas

I'm trying to dive into learning about using a canvas, but I just can't seem to get this basic code to work. Does anyone know what I might be doing wrong? Link to jsfiddle <canvas id="ctx" width="500" height="500" style="border:1px solid #00 ...

Execute the function when an input has a value

When this element is included in the HTML code, why doesn't an alert appear when I enter the same text into the input field? <input type="text" name="name" placeholder="city"> It seems strange that no alert pops up even if I fill the input wit ...

Is there a "Read more" link to expand the content on the page?

I am struggling to figure out why my JavaScript code for toggling between showing more text and less text is not functioning correctly. My goal is for the user to click on the "Show More" button to reveal additional text, and then be able to click on "Show ...

Tips for preventing the browser from freezing when incorporating a large HTML chunk retrieved through AJAX requests

I've developed a web application that showcases information about various items. Initially, only a small portion of the items are displayed at the top level. Upon loading the page for the first time and displaying these initial items, I make an AJAX r ...

Unable to display images using jquery

Just getting started. I'm attempting to load images from a folder. The HTML and JavaScript files are located in the same directory as the images, yet I am still unable to load them. $(function() { $('#main').append('<img src="plus. ...

Problem with video tag getting stuck

Is there a method to have the video tag remain fixed to the top (or bottom from the element above the video tag)? Currently, when the window is resized, the video element moves away from the top (or bottom). Could this be due to the video tag attempting to ...

Navigating through cpanel to interact with a Button using Selenium in Python

Unable to select the new folder option. Here is what I have attempted: driver.find_element_by_id("li.action-newfolder") driver.find_element_by_xpath("//[a(text(), 'New Folder')]").click() Unfortunately, neither of these methods are successful. ...

Troubleshooting Border Width Problem in Safari Version 9.1.3

It seems like there is an issue in Safari 9.1.3 where the border width on elements with a specific combination of styles is causing unexpected outcomes. Essentially, I am using a transform on an absolutely positioned element to achieve horizontal centering ...

Using line breaks and horizontal scrolling in HTML and CSS

I am struggling with a div that contains multiple small spans. My goal is to implement a horizontal scrollbar that extends up until the line break tags (a-z on each line) in order to view the full content on narrow screens. Although I have applied the &ap ...

Having trouble with the JavaScript DOM rewrite for aligning text?

function modifyTextAlignment(){ document.getElementById("th1").style.textAlign = "right"; } #th1{ text-align:left; } <table> <tr><center><th colspan="2" id="th1">List of Important Emergency Contacts</th><center></tr& ...