Apply CSS styles when clicked and remove them when clicked again

I have successfully set a style when clicking on a div. However, I am looking to reset the style to default when clicked again.

     $('#tray-button').click(function() {
       $('.buttons-content').css('bottom', '160px');
    });

 <div class="buttons-content">
        <div id="tray-button" class="button">
            <div class="white"></div>
            <div class="black"></div>
            <i class="fas fa-grip-horizontal"></i>
        </div>
        <div class="button">
            <div class="white"></div>
            <div class="black"></div>
            <i class="fas fa-share-alt"></i>
        </div>
        <div class="button play-pause play">
            <div class="white"></div>
            <div class="black"></div>
            <i class="far fa-1-1x  fa-play-circle"></i>
        </div>
        <div class="button rotate">
            <div class="white"></div>
            <div class="black"></div>
            <i class="fas fa-expand-alt"></i>
        </div>
    </div>

buttons-content - default bottom: 40;

  1. When click on #tray-button -> bottom: 160px;
  2. When click again on #tray-button -> bottom: 40px; (default)

Answer №1

Illustrate how to create a specific CSS rule:

.box.selected {
   border: 2px solid red;
}

Utilize the .selected class and toggle it on mouseover event:

$('.item').on('mouseover', function() {
   $(this).toggleClass('selected');
});

By removing the class, the element will revert back to its initial style. This approach also ensures easier maintenance as styling is not hardcoded.

Answer №2

This code snippet helps to toggle between two states based on a button click.

$( document ).ready(function() {
var stack = 0
$("#tray-button").click(function()
{
if (stack === 0){
$('.buttons-content').css('bottom', '160px');
stack = 1;
}else{
$('.buttons-content').css('bottom', '40px');
stack = 0;
}

});

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

When CSS media queries are applied, the background color of Div does not fully extend to the edge

Although the problem seems simple, I am finding it difficult to find a solution. This page was created as part of my course. https://i.sstatic.net/DKCva.jpg While in developer mode and trying to resize the screen, I noticed that the background color of ...

Please input numbers only into the designated field

I am trying to create an input field in my form that only accepts numbers when pasted into it. For example: If I have some random characters like W123W000 on my clipboard, the input field should only paste 123000. Note: This solution is currently only fu ...

Arrange 4 divs (children) at each corner of the parent element

Currently, I am facing a challenge in positioning 4 divs in the corners of their parent div. The HTML code structure is as follows: <div class="resize"> <div class="n w res"></div> <div class="n e res"></div> < ...

Is it possible for Skycons to show a duplicate icon?

My current issue involves integrating the JavaScript plugin "Skycons" with the Yahoo weather RSS feed. The problem arises when multiple days have the same weather forecast, as the plugin retrieves icons based on ID rather than class. This prevents me from ...

Content towering over the footer

Can anyone help me figure out how to create a footer that appears behind the content when you scroll towards the end of a website? I've tried looking for tutorials online, but haven't found exactly what I'm looking for. Most of what I'v ...

How can JavaScript be used to rearrange the placement of DOM elements?

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div class="boxes"> <div class="red" style="width: 300px; height: 300px; color: red"></div> <div class="blue ...

Tips for overlaying a webpage with several Angular components using an element for disabling user interactions

I currently have an asp.net core Angular SPA that is structured with a header menu and footer components always visible while the middle section serves as the main "page" - comprised of another angular component. What I am looking to achieve is ...

Tips for creating a mobile-friendly responsive table

I need help with making my table responsive in mobile view. Can someone provide guidance on how to achieve this? Feel free to ask if you have any questions related to my issue. tabel.html I am currently using the Bootstrap framework to create the table ...

Using an image instead of a checkbox when it is selected (Angular 4/Ionic 3)

Is there a way to swap out the checkbox for an image? I want this image to change dynamically based on whether the checkbox is checked or not. Unfortunately, adding a class doesn't seem to do the trick as it modifies all icons in the same column. The ...

Error: ExpressJs cannot locate the requested CSS file

Currently, I am delving into the world of expressJS and nodeJS in an effort to expand my skills. However, I've hit a bump in the road when the server fails to locate the CSS file (resulting in a 404 error) upon loading the HTML page. Surprisingly, the ...

Creating a logo slider using CSS - A step-by-step guide

I attempted creating a logo slider using Owl carousel. Unfortunately, the carousel isn't showing up. Here's how I envision my carousel to appear: https://i.sstatic.net/scDtT.png This is the HTML code I used: <div class="owl-carousel"> ...

Issue with React rendering numbers without displaying div

In my user interface, I am attempting to display each box with a 1-second delay (Box1 after 1 second, Box2 after another 1 second, and so on). https://i.sstatic.net/FdTkY.png However, instead of the desired result, I am seeing something different: https ...

Using jQuery to capture the anchor ID when clicked, and then extracting content from an array using JavaScript

My goal is to create an HTML anchor with a unique ID. When the user clicks the anchor, I want the ID to be passed to JavaScript using the onclick HTML attribute. Then, a JavaScript function will read the ID and display the corresponding content in a div. W ...

To prevent the background image (blue border) from shifting, I need to extract the list item element without causing the image to

Having trouble with the side borders on my navbar. I've used a background image for the border (blue line) in the list item of the navbar. Is there a way to move down two specific list items slightly, like shown in my screenshot? I need to bring down ...

The JQuery loading symbol does not prevent keyboard interactions

I successfully implemented a Jquery busy indicator in my application using the following link: However, I noticed that even though it prevents users from clicking on input elements while loading, I can still navigate to these elements by pressing the tab ...

Staggered Drop-Down Menus

I've created a custom CSS drop-down menu that works well, but I've realized that updating this menu across all the HTML pages on my website would be a tedious task. If I ever need to add a new section and tier to the menu, I'd have to update ...

Flex-wrap functionality in versions of Safari 6 and earlier

As I work on developing my website, I have been using flexbox to assist with the layout. It has been functioning well on most browsers, but I have encountered issues with Safari versions below 6.1. Specifically, I am struggling with flex-wrap as I want the ...

The arrangement of Input and Label elements in HTML can impact the overall style of the

Having some checkboxes and labels, I'm using CSS to style the label when the checkbox is checked: input[type=checkbox]:checked + .CheckLabels {background-color: green;} When my HTML is structured like this: <input type="checkbox" name="C" id="ch ...

What is the best method to achieve a width of 100% for an element (textarea) that has unspecified borders and padding, while also including the borders and padding

Native borders for input controls in various browsers often look more visually appealing compared to those set with CSS. However, the thickness of these native borders and padding can vary across different browsers, making it difficult to predict beforehan ...

Tips on automating the process of moving overflowing elements into a dropdown menu

Challenge Description In need of a dynamic navigation bar, I faced the problem of displaying only the first X items on one line and have the remaining items hidden in a "Show more" dropdown. The challenge was to calculate the width of each item accurately ...