Enhance the menu feature in bootstrap3 by adding the ability to hide or expand it with the More Options and Less Options buttons

I utilized Bootstrap 3 to create a menu with additional options. When the "More Options" button is clicked, it reveals more buttons and ends with a "Less Options" button, hiding the "More Options" button. The reverse happens when the "Less Options" button is clicked.

I was able to achieve this functionality using jQuery script and HTML code. My question is: is there a more built-in method in Bootstrap 3 to accomplish these outcomes? I faced difficulties implementing this feature using data-toggle="collapse" which Bootstrap 3 provides.

jQuery script:

$("#moreOptionsButton").click(function(){
    $("#moreOptionsButton").hide(); 
    $("#extraMenu").show();
});

$("#lessOptionsButton").click(function(){
    $("#moreOptionsButton").show();
    $("#extraMenu").hide();
});

HTML code:

<div class="menuButton" id="moreOptionsButton">
    <div class="row">
        <button class="col-xs-offset-1 col-xs-10 btn btn-lg">
            <span><i class="glyphicon glyphicon-chevron-down"></i>&nbsp;&nbsp;More Options</span>
        </button>
    </div>
</div>

<div id="extraMenu" class="collapse">
    <div class="menuButton">
        <div class="row">
            <div class="col-xs-offset-1 col-xs-10 btn btn-lg">
                <span><i class="glyphicon glyphicon-star"></i>&nbsp;&nbsp;Location</span>
            </div>
        </div>
    </div>

    <div class="menuButton">
        <div class="row">
            <div class="col-xs-offset-1 col-xs-10 btn btn-lg">
                <span class=""><i class="glyphicon glyphicon-star"></i>&nbsp;&nbsp;Contact</span>
            </div>
        </div>
    </div>

    <div class="menuButton" id="lessOptionsButton">
        <div class="row">
            <button class="col-xs-offset-1 col-xs-10 btn btn-lg">
                <span class=""><i class="glyphicon glyphicon-chevron-up"></i>&nbsp;&nbsp;Less Options</span>
            </button>
        </div>
    </div>

Answer №1

To enhance the functionality of your buttons, you can include

data-toggle="collapse" data-target="#extraMenu"
in them:

<div class="menuButton" id="moreOptionsButton">
  <div class="row">
      <button data-toggle="collapse" data-target="#extraMenu" class="col-xs-offset-1 col-xs-10 btn btn-lg">
        <span><i class="glyphicon glyphicon-chevron-down"></i>&nbsp;&nbsp;More Options</span>
      </button>
  </div>
</div>

<div id="extraMenu" class="collapse">
  <div class="menuButton">
    <div class="row">
      <div class="col-xs-offset-1 col-xs-10 btn btn-lg">
        <span><i class="glyphicon glyphicon-star"></i>&nbsp;&nbsp;Location</span>
      </div>
    </div>
  </div>

  <div class="menuButton">
    <div class="row">
      <div class="col-xs-offset-1 col-xs-10 btn btn-lg">
        <span class=""><i class="glyphicon glyphicon-star"></i>&nbsp;&nbsp;Contact</span>
      </div>
    </div>
  </div>

  <div class="menuButton" data-toggle="collapse" data-target="#extraMenu" id="lessOptionsButton">
    <div class="row">
      <button class="col-xs-offset-1 col-xs-10 btn btn-lg">
        <span class=""><i class="glyphicon glyphicon-chevron-up"></i>&nbsp;&nbsp;Less Options</span>
      </button>
    </div>
  </div>

As mentioned on http://getbootstrap.com/javascript/#collapse:

Simply assign control of a collapsible element by adding data-toggle="collapse" and a data-target to that element. The collapse should have the class "collapse" along with a CSS selector mentioned in the data-target attribute. For default open state, add an additional class.

In this scenario, your CSS selector is #extraMenu

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

Transferring information using the $.load method in jQuery

In my HTML5 and JavaScript system, I am utilizing a webservice to retrieve data from the database. I have a single page, index.html, where I load other pages in a <div>. The challenge arises when I need to edit or add new users on a separate page. ...

What is the reason that the larrow image is not set against a yellow background?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> </head> <style type="text/css"> *{padding:0px;margin:0px;} .strip {width:1000px;border:1px solid red;clear:none;height:10 ...

Issue with Caching during Javascript Minification

I Have ASP.Net MVC 3 App. Utilizing YUICompressor.Net for compressing Javascript and CSS files post build with MSBuild. The minimized javascript file is named JSMin.js and the CSS file is CssMin.css. In my master page, I reference these files as shown bel ...

The footer on my webpage seems to be completely incorrect, but I'm struggling to identify what is causing the conflict

I'm puzzled by the issue with the footer on my webpage, especially the strange line of black opacity that shouldn't be there. I'm unsure why this is occurring. You can view the problematic page here. I've also created a codepen with the ...

As the user scrolls down, the navigation becomes obscured by the content

Whenever I scroll down on my website at Bradly Spicer, the navigation menu extends and ends up falling behind the content. This is the script I am using: .menu { position: relative; z-index:9999; } .post { background: #fff; border: 1px solid #ddd; font- ...

Exploring ways to access the parent element within the document object of an iframe

Having an issue with iframes. I'm trying to access the parent object or node from within the iframe content. Here's my code: a.html: <html> <head> <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> < ...

The checkbox display and hide functionality is mistakenly inverted

I have a form set up with an input field that should show or hide depending on whether a checkbox is clicked. Currently, the field is showing and hides when the checkbox is clicked. How can I reverse this behavior so that it works the other way around? ...

There was an issue with the Weather API: {"cod":"400","message":"{vlat} is not a valid floating point number

I have been working on my local weather app project and struggling to retrieve the API coordinates. Here is the link I am using from Open Weather Map: http://api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+long+'& ...

Adding a close button inside a circular background using HTML/CSS

I'm struggling to align the X inside the circle perfectly; they just don't seem to match up no matter what I try. Here's a glimpse: https://i.sstatic.net/OIZik.png html: <div class="messages"> <span class="c ...

Issue with Checkboxlist losing selection in Internet Explorer after postback

Technology: ASP.Net 2.0 Within the confines of a webpage, there are multiple checkboxlist controls that correspond to an equal number of buttons. Initially, all the checkboxlist controls remain concealed. When a user clicks on a button, the respective che ...

Reading a JSON file using Javascript (JQuery)

Struggling to figure out how to handle a JSON file using JavaScript. Here are the details : { "streetCity": { "132":"Abergement-Clemenciat", "133":"Abergement-de-Varey", "134":"Amareins" } } I am attempting to access ...

View Image before Uploading

I have a unique situation with my table. In each row, there is a cell designated for image uploading. I am attempting to display a preview of the uploaded image using the following code: var cell8 = row.insertCell(6); var t8 = document.createElement("inpu ...

Email Form Application: Utilizing NodeJs and Express - Error: URL Not Found /

I'm encountering a "cannot GET" error whenever I try to run my application on a live server using VS Code. My assumption is that the issue lies within my routing configuration, but I'm struggling to identify the exact problem. Any assistance woul ...

Enhance the functionality of bindings in Rails 4 by implementing turbolinks

Although there are similar questions about this issue, mine seems to be caused by having the javascript tag in the head rather than the body. Moving my js outside of the page change block is not an option for me. I stick to Rails convention by organizing ...

What is the proper way to define a class attribute for an HTML element within a PHP file?

I am having trouble writing the class attribute in a PHP file for an HTML element. Here is my code: echo '<div><p class="'.($value->getIsRequire() == 1) ? 'required' : ''.'"> </p> <input type="tex ...

why isn't bootstrap 4 pull-xs-right functioning properly?

I am utilizing Bootstrap 4 utilities to align buttons to the right of the screen in my HTML, and it is achieving the desired effect. However, I am facing an issue where the buttons are not pushing down the content below them as anticipated. Instead, they ...

CSS - owl carousel automatically stretches images to full width

Here is the code snippet that I am working with: $(document).ready(function() { $('.owl-carousel').owlCarousel({ loop:true, items:4, autoplay:true, autoplayTimeout:2000, autoplayHoverPause:true }); }); #owl-demo ...

Order of callback execution in jQuery's ready function

When two JavaScript functions on a page need to be called once the document load is complete, is there a possibility that one function could be executed before the other, or will it always be the same order? For example, using jQuery with the following co ...

The button inside the popup is not responding to the click event

I am developing a jQuery popup window for my ASP.Net application that opens when a button is clicked. The code below shows how I am implementing the popup: HTML code: <%-- Popup --%> <div id="modal_dialog" class="PopupStyle" styl ...

Is it possible to close the menu by clicking outside a div or letting it disappear on mouseout after a set period of time?

When visiting a website, you may notice menus that display sub-menus when hovered over. These menus and sub-menus are created using div elements, with the sub-menus hidden initially using style.display = none; The issue arises when a sub-menu is opened an ...