Implementing CSS functionality to enable and disable a button using JQuery upon user clicks

I'm currently exploring a solution to have the LeaderboardButton change its DIV to include bottom border radius upon clicking (which it does). However, I'm wondering how to make it so that upon clicking again, the border radius resets back to none. In other words, you click once and get border radius, then click again for no border radius. Click for border radius again, and so on - an ON/OFF functionality for CSS radius settings.

Below is the code snippet:

$('#left-container').click(function(){
    $('#leaderboard').slideToggle( "slow", function(){  
        $('#leadeboardButton').css({borderTopLeftRadius: 10, 
             borderTopRightRadius: 0, 
             borderBottomLeftRadius: 10, 
             borderBottomRightRadius: 10,
             WebkitBorderTopLeftRadius: 10, 
             WebkitBorderTopRightRadius: 0, 
             WebkitBorderBottomLeftRadius: 10, 
             WebkitBorderBottomRightRadius: 10, 
             MozBorderRadius: 10 
        }), function(){
$('#leadeboardButton').css({borderTopLeftRadius: 10, 
     borderTopRightRadius: 0, 
     borderBottomLeftRadius: 0, 
     borderBottomRightRadius: 0,
     WebkitBorderTopLeftRadius: 10, 
     WebkitBorderTopRightRadius: 0, 
     WebkitBorderBottomLeftRadius: 0, 
     WebkitBorderBottomRightRadius: 0, 
     MozBorderRadius: 0
     });
                    }   
});

Answer №1

Consider moving the border styling to your CSS file for better organization. Create a class named "border" for styling purposes, which will simplify your JavaScript code.

$("#leftcontainer").click(function() {
    if ($("#leftcontainer").hasClass("border")) {
        $("#leftcontainer").removeClass("border");    
    } else {
        $("#leftcontainer").addClass("border");
    }
});

You can also check out this JSFiddle example: JSFiddle Demo

Answer №2

To achieve this, consider creating two distinct CSS classes with unique styles. After each button click event, determine the current class assigned to the button and switch it to the alternate class.

Utilize the jQuery function toggleClass to easily toggle between the two CSS classes.

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

"Exploring the capabilities of speech recognition in Ionic 4

Having some trouble with the Speech-recognition feature in my Ionic 4 app. Here is a snippet from my code: import {Component} from '@angular/core'; import {SpeechRecognition} from '@ionic-native/speech-recognition'; @Component({ se ...

Why does my array become empty once it exits the useEffect scope?

const [allJobs, setAllJobs] = useState([]); useEffect(() => { axios.get('http://localhost:3002/api/jobs') .then(res => setAllJobs(res.data)); allJobs.map((job, i) => { if (job.language.toLowerCas ...

Styling: petite vibrant square positioned in the corner of a large container

Can you please take a look at the screenshot from the demo app: box over box image I am trying to create a colorful small square box that will appear in the top left corner, on top of the larger parent square box (including all the contents inside the whi ...

Updating MySQL records with Node.js

Currently, I am extracting data from an API and organizing it into an array ([arr1]). I am utilizing node-mysql to interact with my database and insert the fetched information. Everything is functioning smoothly when inserting new entries into the databas ...

Increasing the font size within a specific div container

I need assistance with increasing the text size using style in this code snippet. <div class="hidden-overlay" style="font-size: larger"> MASTER CLASS AIR 2.0 TITANIUM </div> ...

What is the process for incorporating an external script into a Vue component?

Seeking assistance urgently... I am encountering an issue with a Vue component and an endpoint that provides a script containing a small menu with actions. However, once the script is loaded, the actions do not seem to function on the page and I cannot det ...

Adjust the transparency level when hovering in an R Shiny application

I have an absolute Panel in my R shiny app and I am attempting to adjust its opacity based on the mouseover action. The goal is to have the opacity at 1 when the cursor is on the panel, and 0.5 otherwise. However, the code I tried ended up making the pane ...

Tips for adjusting the placement of the "create account" checkbox on the Woocommerce checkout page

I am looking to reformat the layout of my checkout page. Currently, the "create account" checkbox appears after the email field. My goal is to move the create account checkbox inline with the email field, positioned to the right of it. Although I can succ ...

Having issues with opening an exported Excel file using ExcelJS

While working on my JS project, I utilized the ExcelJS library. Everything was functioning smoothly with English characters. However, when I switched the language of my application to French and attempted to export the content as an Excel sheet, I encounte ...

Verify whether the template's form field is vacant

One issue I am facing is related to a form field named topics, which is a ManyToManyField. When calling {{form.topics}} in the template within a div inside a fieldset, I need to determine if the field is empty or if its length is less than or equal to 1. I ...

Utilizing Callback Functions within Swift Modules in React Native

I'm currently working on creating a native swift module that includes a function I need to call in my javascript code. At the moment, I have three key files for this setup: module.swift, module.m, and the bridging header. This is what MyModule.swift ...

Error: The function $rootScope.$on is not defined within the Connectivity Factory

I am facing an issue with my Ionic app while trying to run it in offline mode. The error message "services.js:392 Uncaught TypeError: $rootScope.$on is not a function" appears when I launch the app. Can someone please explain why this is happening and what ...

Passing data from child to parent in Angular using EventEmitter

I have integrated a child grid component into my Angular application's parent component, and I am facing an issue with emitting data from the child to the parent. Despite using event emitter to transmit the value to the parent, the variable containing ...

Searching for a particular Prettier setting

Seeking a Nicer alternative. I am exploring if there is a way to format code like this without moving the first attribute to a new line: <div class="test-class" [test-binding]="true" (test-binging)="test()" ...

Attempting to trigger the onchange function established by jQuery

Within my code, I have a change listener set on the checkboxes in this section: $(".section").change(function() { if($(this).is(":checked")) { $("." + this.id).show(); ... Now, I am attempting to simulate a click event on the ch ...

Is it possible to integrate applications developed using (JS, Node.js, Express, and MongoDB) into a Wordpress site?

After successfully building projects using JavaScript, Node.js, Express and MongoDB, I am now looking to incorporate them into my existing WordPress website. Is it possible to integrate these projects into WordPress, or would it be better to create a sep ...

Using objects as a React child is not allowed. If you intended to display multiple children, consider using an array instead. What is the proper way to render this?

Based on my research, it appears that the error arises when attempting to display '{menu.props.options[0]}'. I understand that it cannot be returned in this format and needs to be wrapped in an object or array. The code causing the issue is as f ...

When attempting to modify an object that was initialized outside of a function, I am unable to change its value, or sometimes the function may not run at all. This

Currently in the process of constructing a social network utilizing nodejs, my knowledge on the subject matter is quite novice. This marks my initial discourse on the topic, and I would greatly appreciate your comprehension. Within my social network frame ...

Update the URL and parse the data in a Backbone collection

For the purpose of development, I am looking to replace the collection with fake JSON results either from a JSON variable or a JSON file. However, I encountered an error when attempting to do so using url http://jsfiddle.net/qhoc/uZhM8/ GET http://fiddle ...

Attempting to isolate SQL outcome in order to adjust price based on a percentage

I am currently working on a SQL search and results page that displays 3 columns of data: category, number, and price. However, the results are returned all together in one big lump. My goal is to have a dropdown list with percentages, where clicking on one ...