Creating a toggle button for a div element to expand and collapse its content: Step-by-step guide

For my e-commerce site selling musical instruments, I'm designing a product landing page that showcases guitars, keyboards, violins, and cellos. As part of the design, I want to include expandable sections with detailed information about each instrument.

To achieve this, I have added specific styling and functionality using CSS code snippets for each instrument category - guitar, keyboard, violin, and cello. Each section includes a brief description, an image, and a link for more information.

The challenge I'm facing is implementing a "show more" button below the keyboard section, which would reveal additional content when clicked. Despite searching for solutions online, I haven't been able to find a suitable answer yet.

Answer №1

@Alok Marathe, kindly review the code below for your specific issue. Many resources on stackoverflow can provide insight into how to implement a show more functionality. Examples with working solutions are available for reference.

For additional details, please refer to this Stack Overflow link and examine the provided code snippet:

stack-overflow

jQuery(document).ready(function ($) {

    // This will execute upon page load when the document is ready
    if ($('.product-info-box').length > 2) {
        $('.product-info-box:gt(1)').hide();
        $('.showmore').show();
    }

    $('.showmore button').on('click', function () {
        // Toggle elements with class that have an index greater than 2
        $('.product-info-box:gt(1)').toggle();
        // Change text of show more element for demonstration purposes
        $(this).text() === 'Show less' ? $(this).text('Show more') : $(this).text('Show less');
    });

});
/* Sample CSS - No implementation needed */
* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

/* Your CSS Styles Here */

.product_info_scroll {    
    /* Fixed div height */
}

.showmore {
    text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="product-info" class="product-info">
        <b>Instruments our brand serves: </b>
        <div class="product_info_scroll">
            
            <div class="guitar product-info-box">
                <div id="guitarbox">
                    <div id="guitarcontent">
                        <h1>Guitar</h1>
                        <p>Description here...</p>
                    </div>
                    <div id="guitarimg">
                        <a href="" target="_blank">
                            <img src="/images/electric-guitar.jpg" width="500px">
                        </a>
                    </div>
                </div>
            </div>

            <div class="keyboard product-info-box">
                <div id="keyboardbox">
                    <div id="keyboardcontent">
                        <h1>Keyboard</h1>
                        <p>Description here...</p>
                    </div>
                    <div id="keyboardimg">
                        <a href="" target="_blank">
                            <img src="/images/keyboard2.png" width="500px">
                        </a>
                    </div>
                </div>
            </div>

            <div class="violin product-info-box">
                <div id="violinbox">
                    <div id="violincontent">
                        <h1>Violin</h1>
                        <p>Description here...</p>
                    </div>
                    <div id="violinimg">
                        <a href="" target="_blank">
                            <img src="/images/violin.jpg" width="500px">
                        </a>
                    </div>
                </div>
            </div>

            <div class="cello product-info-box">
                <div id="cellobox">
                    <div id="cellocontent">
                        <h1>Cello</h1>
                        <p>Description here...</p>
                    </div>
                    <div id="celloimg">
                        <a href="" target="_blank">
                            <img src="/images/cello2.jpeg" width="500px">
                        </a>
                    </div>
                </div>
            </div>

        </div>

        <div class="showmore">
            <button type="button">Show More</button>
        </div>
    </section>

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 about creating a fresh variable in Assemble or merging two comparison helpers together?

Is it possible to create a new variable within a partial in Assemble (assemble.io) or combine two comparison helpers? For example: {#is somevar "yes" || anothervar "no"} In my partial, I have HTML that should only display if one of two different variable ...

Could jQuery and CSS be utilized for image enlargement?

Is it possible for jQuery (or CSS!) to detect an area around the mouse when hovering over an image, capture the image underneath, and then zoom in on that area? This concept differs from typical jQuery zoom effects where there are two separate images - sm ...

Click once to expand all rows in the table with ease

I have successfully created a basic table using HTML. The table includes nested elements that are designed to open individually when clicked on the assigned toggle. Essentially, clicking on a '+' icon should reveal a sub-menu, and clicking on a & ...

Unable to view videos shared by users in PeerJS and WebRTC video chat application from different tabs

Recently, I embarked on the task of creating a Video chat Website using Peer Js. Initially, everything seemed to be working fine as I was able to see my own video stream. However, a problem arose when attempting to view the video stream from another tab or ...

Re-rendering a Highcharts chart for optimal display

I have implemented Highcharts to showcase a set of data through different types of charts - Area chart, Line chart, and Bar chart. By default, the page loads with the Area chart displayed. Upon clicking a button, I switch to displaying the Line chart and t ...

What could be causing the 404 Ajax not found error in my script?

I'm facing an issue with my code. When I try to run it, I encounter the following error: 404 (Not Found) while using the get method. Here is my html code snippet. <html> <head> <title>Issue</title> <meta charset= ...

How can I replace the unsightly "Web page not available" error in WebView on Android with a more visually pleasing alternative?

In my WebView Activity, I sometimes encounter issues with loading properly when the WIFI/DATA connection is poor or disconnected. This could potentially be a common occurrence once my app is in use. I'm curious to know how I can replace the unattracti ...

When moving to a different page, PHP seems to have a problem keeping the session values in place

As I work on my PHP application, I encounter an issue with maintaining session values. In this scenario, I have two files: 'sidebar.php' and 'home.php'. The sidebar is included on the home page, and it contains login controls. Logging i ...

What is the best way to align text in the middle on smaller devices like phones and iPads?

I have been working on a login screen and encountered a problem with the layout on smaller devices. Despite centering all the content perfectly on my computer's browser, when I inspect it using developer tools for smaller devices, everything shifts to ...

Vuetify Card Overflow: Handling Multiline Text with Ellipsis

I have been experimenting with Vuetify cards and encountered an issue with their height. In the image below, you can see that the cards' height varies until it reaches a specified max-height of 225px. I want to implement a text-overflow: ellipsis feat ...

An error occurs in the console stating 'Maximum call stack size exceeded' when trying to add data to the database using JavaScript

I've been struggling with a JavaScript error for the past few days and despite scouring through numerous answers on StackOverFlow, none seem to address my specific issue. My goal is to simply submit a record to the database using a combination of Jav ...

Exploring Angular 2 Tabs: Navigating Through Child Components

Recently, I've been experimenting with trying to access the HTML elements within tabs components using an example from the Angular 2 docs. You can view the example here. Here is a snippet of my implementation: import {Component, ElementRef, Inj ...

Trigger a notification based on the selected choice

Here is some sample HTML code: <div id="hiddenDiv" style="display: none;"> <h1 id="welcomehowareyou">How are you feeling today?</h1> <select name="mood" id="mood"> <option value="" disabled selected>How are you feeling?</o ...

Using Vue.js to connect v-html to a custom CSS stylesheet

I am currently working with HTML generated by a function on an external server and I am able to preview this within a tag. Additionally, I can retrieve the CSS information in a similar manner. <template> <div v-html="html"></div ...

Issue with setTimeout function when used in conjunction with an ajax call

Currently, I am developing a quiz portal where questions are organized in modules. Each module consists of 5 questions: the first 4 are text-based, and the 5th is an image-based question. Upon registering through register.php, users are directed to index. ...

How can I enable two-finger scrolling on mobile web browsers?

I am currently tackling a project that involves dragging operations on a canvas, making scrolling with a single finger impractical. My goal is to enable scrolling using two fingers instead. After testing different touch-action values, I discovered that pi ...

Steps to open and configure a Mobile-Angular modal from another controller

Is it possible to use a modal in Angular JS for mobile devices without compromising the layout? I'm having trouble setting up the controller for my modal inside my main controller and passing parameters. Should I consider using ui-bootstrap for this p ...

JavaScript function to copy the first row of a table to the clipboard

I am new to JavaScript and I'm facing an issue with copying cells in a table. I have successfully created a table based on my MySQL database, but when I try to copy a cell using the copy button, only the first cell gets copied. I understand that I nee ...

What could be the reason behind the disappearance of text from the previously highlighted button in my calculator's "button grid" when I change the highlighted button?

Currently, I am in the midst of creating a tip calculator with a grid consisting of various percentage buttons. My main objective is to change the font and background color when any tip button is selected. Nevertheless, an issue has surfaced - whenever I h ...

Adjusting the size of a Video/Image Slider to perfectly fit the screen dimensions (both width and height

I have been using a slider code to display my images and videos. The code can be found here: Code:https://codepen.io/1wdtv/pen/jbjZeb The issue I am facing is that the slider is only responsive in terms of width. This means that when resizing the browser ...