Can we show the dropdown with the active button?

Can someone assist me in transforming my sidebar buttons into dropdowns when viewed on a responsive layout? I'm looking for something similar to the sidebar (on the left) featured on this website:

If possible, I would like the dropdown to only display the active button initially, and then reveal the other options when the active button is clicked. Similar to the functionality on the website mentioned above, but in my case, the buttons are toggled as active upon clicking, rather than loading a new page.

Below are the links that need to be converted:

<div class="buttonRow">
        <ul class="btn">
        <label for="drop" class="toggle">Active Link Here</label> ***only visible in dropdown mode***
            <button class="btnAbt2"><li> Link 1 </li></button>
            <button class="btnAbt2"><li> Link 2 </li></button>
            <button class="btnAbt2"><li> Link 3 </li></button>
            <button class="btnAbt2"><li> Link 4 </li></button>
            <button class="btnAbt2"><li> Link 5 </li></button>
        </ul>
    </div>

I have successfully implemented adding an active class upon click. Now, I just need that active class to be the main text displayed in the dropdown menu.

For a visual reference, please see this image: here

Your help is greatly appreciated. Thank you!

Answer №1

Is this the solution you are aiming for?

$(document).ready(function(){

  $('.toggle').on('click', function(){
       $('button').toggle();
  });
  // declare a variable for text
  var $text;   
  $('.btnAbt2').click(function(){ 
       // fetch the text from the clicked link
       $text = $(this).text()
       $('.btnAbt2').removeClass('active'); $(this).addClass('active');
       // update the label text
       $('.toggle').text($text);
  });
 
});
.buttonRow {
  width: 100%;
}
.btn {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
.toggle {
  width: 100%;
  padding: 5px;
  background: red;
  display: block;
}
button {
  width: 100%;
  padding: 5px;
  background: #CCC;
  display: none;
  border: 0;
}

@media screen and (min-width: 500px) {
    .toggle {
       display: none;
    }
    button {
       width: auto;
       float: left;
       display: block;
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="buttonRow">
        <ul class="btn">
        <label for="drop" class="toggle">Active Link Here</label> 
            <button class="btnAbt2"><li> Link 1 </li></button>
            <button class="btnAbt2"><li> Link 2 </li></button>
            <button class="btnAbt2"><li> Link 3 </li></button>
            <button class="btnAbt2"><li> Link 4 </li></button>
            <button class="btnAbt2"><li> Link 5 </li></button>
        </ul>
    </div>

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

Attempting to save data to an external JSON file by utilizing fs and express libraries

I've encountered a challenge while attempting to serialize an object into JSON. Despite my best efforts, I keep encountering an error that has proven to be quite stubborn... Below is the code snippet that's causing the issue: APP.post('/api ...

Transferring a PHP variable from a dropdown menu to query the database

I'm currently working on a project for one of my classes and I've run into an issue regarding passing a variable from a Chosen selector. While the $inst variable is being passed successfully, I'm encountering an error with the $focus variabl ...

Tips for ensuring consistent display of UTF-8 arrow characters across different web browsers

Is there a way to ensure consistent display of UTF-8 arrow characters across all browsers? I have experimented with various font sizes like px and pt, as well as using HTML Entity codes (e.g. &#9664;) but unfortunately the arrows still show difference ...

Using SVG files as properties in React from a data.js file

I have a website where I store my content in a data.js file and pass it to my components using props. Everything is working correctly, except for my .svg files. When I try to display them, they do not appear. However, if I change the extension of the image ...

Getting the value of a file input type on an Ajax or JQuery page using PHP

When working with Ajax / JQuery to pass variable values from input fields like text, textarea, and select to a PHP process page, everything functions correctly. However, there seems to be an issue when trying to retrieve the value of an input type file var ...

The error message "bind this is not defined in react" is displayed when attempting to read

Is it necessary to use bind in the constructor if you already have bind(this) in JSX? render(){ return( <input onChange={this.myFunc.bind(this)} type="text"/> ) } myFunc(){ alert('should trigger'); } I encountered an erro ...

Error: module 'next/react' not found

Recently delving into next.js, I encountered an error after creating a project with npx create-next-app. Oddly enough, the app still runs despite this issue. { "resource": "/E:/codes/news-website/news-website/pages/index.js", ...

Issue with Three JS where the BoundingBox does not update correctly following rotation operations

I am trying to determine the bounding box of a geometry after applying rotations to it. I obtained the rotation code from the sample editor in Three JS: object.rotation.x = xRadians; object.rotation.y = yRdians; object.rotation.z = zRadians This rotatio ...

What significance do the blank quotes hold in terms of value?

Is it possible to enter multiple values for the "favoriteCities" field since the value is empty? <input name="favoriteCities[]" value=""/> ...

Is there a way to lock the eye icon in place within the password field using Vue.js?

I added an eye icon to the right side of my password input fields. However, occasionally error messages may pop up on the page to signify issues with the input fields. When these error messages appear below the relevant input field, the position of the eye ...

Struggling to delete items from an array in react.js

I am attempting to remove an item from a nested childArray within another Array. This is my current approach: const childArrayHandler = (childData, sub, questionId, data, btnId) => { // Manage color change on click const isInList = selectedBtnL ...

Exploring the integration of foreign languages within C# code?

In search of a C# expert who can assist me with a question I've been struggling to find an answer for all day. Google searches have yielded outdated information, and I need fresh insight. Wondering if there's a way to incorporate CSS and HTML in ...

Enhancing functionality with jQuery: updating multiple input fields at

Currently, I am attempting to utilize jQuery to modify some HTML text by adjusting a slider. I have managed to accomplish this; however, I also need it to happen only if a checkbox is checked. How can I integrate both conditions and ensure that the text ch ...

Splitting a loading button within post.map() in React for like/dislike functionality

I'm currently working on implementing a like/dislike feature in React. The issue I've run into is that when I try to like or dislike a post, the like/dislike buttons on all other posts are stuck in a loading state. This means that while I'm ...

Trouble fetching asynchronous data in Next.js Higher Order Component

Currently, I am in the process of creating a Higher Order Component (HOC) that will fetch user data from my API and then provide props to the wrapped component. This will allow the component to redirect the user based on their role. Although the HOC appea ...

Using jQuery AJAX to send a URL as a string parameter

Currently, I have an ajax function that sends a string of variables to my script. However, there is one variable that needs to hold a complete URL along with parameters. In the current setup, var1 and var2 are received as $_POST variables. But I specifica ...

Typescript: Add a new variable preceding a specified string

fetchArticle(articleId: string): Observable<any> { return this._http.get(`${this._url}/${articleId}`) .map((response: Response) => response.json()) .do(value => console.log(value)) .catch((error) => Observable.throw(err ...

Do you know if there is a setting in prettier that allows line breaks to be preserved?

Encountering an issue with the prettier extension in VS Code, Whenever I enter the following code: const result = await pool .request() .query('select NumberPlate, ID, TimeStamp from RESULTS order by ID'); and save the file, it con ...

What is the best way to ensure the menu background aligns perfectly with the header in HTML/CSS?

Issue: Menu background doesn't align with header. (visible in the image provided below) VIEW IMAGE Any suggestions on how to resolve this alignment problem? CSS CODE : .header { margin:0px auto; max-width: 960px; } #header { height:30 ...

The overflow:hidden feature doesn't appear to be functioning as expected

I am struggling to hide the text details within a div containing text and image, organized in ul li structure. Despite applying overflow hidden to the .sbox class, the text details refuse to remain hidden and appear to be overflowing. Explore this issue o ...