Is there a way to include a collapsible button specifically for mobile users?

I am looking to display just the list on large screens without the collapsible button, but I want the button to appear on mobile devices. I have attempted using hidden-lg or visible-xs classes on the button tag, but it ends up removing the entire row for larger screens. Is there a way to only display the collapsible button on mobile? Thank you in advance!

 <button id="button" type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
      <span class="glyphicon glyphicon-collapse-down"></span> Show
    </button>

    <div id="demo" class="collapse">
      <ol class="list-group">
          <li class="list-group-item">Warrior</li>
          <li class="list-group-item">Adventurer</li>
          <li class="list-group-item">Mage</li>
      </ol>
    </div>

custom.js

$(function(){
  $('#demo').on('hide.bs.collapse', function () {
    $('#button').html('<span class="glyphicon glyphicon-collapse-down"></span> Show');
  })
  $('#demo').on('show.bs.collapse', function () {
    $('#button').html('<span class="glyphicon glyphicon-collapse-up"></span> Hide');
  })
})

I've also tried

#demo{
    display: none;    
}

@media (min-width: @screen-md-min) {
    #demo{
        display: block;    
    }
}

Answer №1

Hey @Deebs, I took your proposal and added more to it because I think that on mobile devices, the expected behavior would be for the #demo div to start off hidden and only show up when you click on the #button.

// Vanilla JS
document.getElementById("button1").addEventListener("click", function() {
    var displayStatus = document.getElementById("demo1").style.display;
    document.getElementById("demo1").style.display = displayStatus === "" || displayStatus == "none" ? "block" : "none";
    this.innerHTML = displayStatus === "" || displayStatus === "none" ? "Hide" : "Show";
});

// Using jQuery
$("#button2").on("click", function() {
    $("#demo2").toggle();
    $("#button2").text( $("#demo2").css("display") === "block" ? "Hide" : "Show" );
});
#demo1, #demo2 {
    display:none;
}
#button1, #button2 {
    display:block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p><strong>Vanilla JS:</strong></p>
<button id="button1" type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo"> <span class="glyphicon glyphicon-collapse-down"></span> Show</button>
<div id="demo1" class="collapse">
    <ol class="list-group">
        <li class="list-group-item">Warrior</li>
        <li class="list-group-item">Adventurer</li>
        <li class="list-group-item">Mage</li>
    </ol>
</div>

<p><strong>jQuery:</strong></p>
<button id="button2" type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo"> <span class="glyphicon glyphicon-collapse-down"></span> Show</button>
<div id="demo2" class="collapse">
    <ol class="list-group">
        <li class="list-group-item">Warrior</li>
        <li class="list-group-item">Adventurer</li>
        <li class="list-group-item">Mage</li>
    </ol>
</div>

Answer №2

The wording in your inquiry is a bit unclear... and the media query seems a bit off. Give this code snippet a try:

#demo{
    display: none;
}

@media screen and (min-width: 780px) {
    #demo{
        display: block;    
    }
}

With this code, the demo section will stay hidden on screens smaller than 780px and visible on screens wider than 780px. If you want to hide or show the button itself, additional CSS rules would be necessary.

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

Encountering a problem retrieving MongoDB data using "Ref" in Mongoose

Hello, I've been experimenting with GraphQL and have encountered a situation where I'm stuck in the "Ref" part. Let's say I have two schemas defined as follows: User.js const userschema = new Schema({ email: { type: String, requi ...

Ways to display JSX with formatted new lines and spaces in React.js?

Here is an image displaying a comparison between my console log and React website in Google Chrome. Check out the Console log and Chrome website Notice how the console output is neatly formatted with new lines and indents, while the text below "Generated ...

Implementing a personalized surcharge for a specific choice during the checkout process in WooCommerce

Within the WooCommerce Checkout page, a dropdown custom field for "Card type" has been incorporated with various options. Some of these options are free while others have an associated fee of $0.99. Although the fee is accurately calculated on the checkout ...

JavaScript: Modify the dropdown class with a toggle option

Hi there, I'm currently facing a small issue and I could really use some assistance. I have a dropdown menu with two selection options - "green" and "blue", and I need to toggle a class based on the selected option. If you'd like to take a look, ...

How to extract specific options from a Vue.js multi-select dropdown

I've been struggling to retrieve values from a multi-select box using Vue. My goal is to capture the selected values and submit them to a data source, but I haven't had any success so far. Below is a snippet of my code: <div id="app"> & ...

How to effectively use jQuery to center images of varying widths horizontally within a div element

Presented below is the div in question: <div id="photos"> <div id="photos_cycle"> <img id="photo" src="{{ asset('bundles/tcheetenweb/images/photos/female_tennis_player.jpg') }}" /> <img id="photo" src="{{ asset(' ...

Convert query strings into objects using Node.js

Looking for assistance with parsing a GET request that is structured as follows: /api/stores?offset=10&limit=15?order=+name?filter=name='Tesco|distance=4 I am in need of help to parse the +name query parameter into an object formatted as {name: ...

"Concealing specific choices in Autocomplete with reactjs: A step-by-step guide

I am looking for a way to hide the selected option in a Dropdown menu so that it does not appear in the next dropdown. For example, if "Hockey" is selected in the first dropdown, it should not be displayed in the second dropdown; only "Baseball and badmint ...

"Enhance User Interaction with a Bootstrap Popup when Submitting Form Data via

As a junior web master, I have a simple question to ask. I have created a single page application for a client with a contact form at the end of the page. The validation is done using Bootstrap, but the only method I know to send the form data to a mail id ...

Is jQuery causing the table to exceed the page width?

I have a table in my HTML document that is displaying too many columns in a single row (~25), causing the table to stretch beyond the size of the page and creating unwanted scrollbars. I am seeking a solution to shrink the content of each column while also ...

keep the color of the link after clicking until a different link is clicked

At the bottom of every post on my website, there are 3 buttons: "Written By", "Related Post," and "In This Category": SoCatchy! I'm looking to have each button change color when clicked, and stay that way until another link is clicked. Here is the c ...

Creating custom components in React

At times, I find myself in need of a special UI component such as a multiple range slider. However, I have a preference for not relying on third-party libraries, so I usually opt to create the component myself. As time has passed, I have completely stopped ...

The event.pageY position consistently reflects the laptop screen size rather than the exact position where the click occurred

My webpage is scrollable, but the event.pageY position always corresponds to my screen size. Even when scrolling down and clicking near the top of the screen, it registers as 50px. I am currently utilizing event.pageY While this functions correctly on a ...

"Proceeding to" express this redirection

Greetings, I am currently struggling to understand why res.redirect('/index') is rendering like this: <p>OK. Redirecting to <a href="/index">/</a></p> Instead of directly redirecting on the page. I searched through th ...

Exploring the versatility of orderBy and filter in handling cross-referenced content across multiple JSON objects

Is there a way to filter and search for the item name when the item details are sourced from another JSON object? Demo: http://codepen.io/anon/pen/GjAxKX While the rest of the filtering and ordering functionalities are functioning correctly, I am struggl ...

Placing two images next to each other with accompanying captions

Currently working on developing a webpage, I am aiming to have two images in each row with captions positioned beneath them. The images are already configured as follows, I just require the captions: <div class = "container"> <img class = "pi ...

Passing numerous PHP echo values to multiple text fields through jQuery AJAX

Check out my PHP file where I will be returning two echoes using jQuery Ajax <?php $data=$_POST['data1']; $data2="h"; if($data2==$data) { echo "john"; echo "usa"; } else { echo "Error"; } ?> This is how I call it with Ajax and populate ...

When an icon is hovered over in Vue, a text box popup will be displayed

Working on a personal project that requires obtaining the status of all devices within a unit. The status, along with the device name, is returned in an array from the function deviceStatus(). If all devices are currently marked as ON, the home icon next t ...

Tips for eliminating the horizontal scroll bar on a responsive background image

I'm having an issue with a full-width div containing a background image on my responsive website. Everything looks fine on desktop, but when I switch to responsive mode using Chrome Dev Tools, a horizontal scroll bar appears that I can't remove w ...

DataTables ajax feature is not functioning

Looking to implement AJAX in this table using the following code snippet: $('#example').DataTable({ 'serverSide': true, "bPaginate": false, "info": false, "iDisplayLength":20, "bLengthChange":false, 'ajax&a ...