Deactivate toggle effect by clicking on a different link

My existing JavaScript code changes the background color of a link when it is clicked:

$(".button").click(function(){ 
    $(this).css('background-color', '#555');
});

While this functionality works, I am looking to have the color toggle off when another link is clicked.

Answer №1

Here's a cool trick you can try out

$(function() {
  $(".button").click(function() {
    $(this).css('background-color', '#555');
    $(".button").not($(this)).css('background-color', '');

  });
});

Check it out on JSFIDDLE!

Answer №2

One way to switch a class on and off is by toggling it.

.toggled {
     background-color: #555;
}

//Implementing the toggle functionality in JavaScript
$(function() {
    var allButtons = $(".button");
    allButtons.click(function() {
        allButtons.removeClass("toggled");
        $(this).addClass("toggled");
    });
});

Answer №3

To change the background of a clicked button and remove the background of another element, you can utilize the .siblings() method to select the sibling elements of the clicked button.

$(".button").click(function() {
    $(this).css("background-color", "#555").siblings().css("background-color", "");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="button">Button1</a>
<a href="#" class="button">Button2</a>
<a href="#" class="button">Button3</a>

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

Unable to get jQuery date picker to function on dynamically generated input fields through ajax

I'm facing an issue with my jQuery date picker not working on an input field generated via Ajax from the server-side, but it works fine when the field is directly added to the page. Here's a simplified version of what I'm dealing with: The ...

What is the best method to insert an item into a SQL database using jQuery and AJAX through a web service?

I have a web service that contains two methods (select, insertdata). I am trying to insert a record into a SQL database using jQuery, but my current code is not working. Can someone please assist me with this? Description of My Web Service: SqlConnection ...

Extracting Data: Unlocking Information from Websites

My friend and I are working on a school project that involves creating a small application. The main goal of this app is to find pharmacies in our area. I am looking to extract information from two specific websites. gun = day/date lc = id of town ...

Node.js: Attempting to arrange ISO dates based on their time span

I am currently working on creating a line chart using the chart.js library. In order to do this, I have an array that contains ISO dates and my goal is to determine which ISO dates belong to the same hour and day. This information will then be used to gene ...

Tips for aligning sections of a navigation bar in different directions: left and right

I'm attempting to float a logo to the left side of a nav bar and have the text float to the right of the nav bar using Bootstrap. I've tried adding float and text align classes to outer divs and specific elements but haven't had success yet. ...

Issues persist with debugger functionality in browser development tools following an upgrade from Angular 8 to version 15

After upgrading from Angular version 8 to version 15, I've encountered an issue where the debugger is not functioning in any browser's developer tools. Can anyone provide some insight on what could be causing this problem? Is it related to the so ...

Leveraging $(this) in jQuery

I am new to jquery and JavaScript, so please bear with me if this question seems basic. I am designing an artist bio page for a conference, which can be viewed live on . When a user clicks on an artist's image, a colorbox opens up displaying the arti ...

JQuery receives an enchanting response from the magic line

I could really use some assistance with this problem. I've managed to make some progress but now I'm stuck! Admittedly, my JQuery skills are not that great! Currently, I have a magic line set up where the .slide functionality is triggered by cli ...

What is the best way to start tiny-slider automatically once the video has ended?

I am currently using the tns-slider plugin and have a setup with 3 slides (2 photos and 1 video). <div class='tiny-slider'> <div class='slide slide1'> <div class='video-slide'> <video id=&qu ...

Generate a Vue component that automatically wraps text dynamically

Challenge I am looking for a way to dynamically wrap selected plain text in a Vue component using the mouseUp event. For instance: <div> Hello World! </div> => <div> <Greeting/> World! </div> Potential Solution Approach ...

Integrating the Google Translate tool onto a website

My goal is to integrate a translation tool like Google Translator or other reliable options onto my website. I am not looking to translate the entire content of my pages into another language, but instead want to add a feature similar to the one found on t ...

Error: $e.data() is incompatible with yii.confirm box

After upgrading to yii 2 stable, the code I used for the confirm box is no longer working. The error message I am now encountering is: "$e.data() is not a function." Upon further investigation, it seems that the type of $e is a function. yii.allowAction ...

angular and node: troubleshooting the $http.get error

I have encountered an issue with the $http.get instruction. Without it on the main page, I receive a result of "random 46" which is correct. However, when I include $http.get, I get a result of "random {{ number }}". How can this problem be resolved? -se ...

Switching perspective in express with Pug

Hello everyone, I'm still getting the hang of using Node.js with Express and Jade. I've successfully set up a basic 1 page app where a login page is displayed by default. Once the user logs in and is authenticated against a database, the function ...

Perform an Ajax Get Request every time a user enters a key in the search field

I'm currently working on implementing a real-time search box in my Django Application. The idea is that when a key is pressed in the search field, it should trigger a GET request to the server. Upon success, only the data relevant to the search query ...

What is the best approach for accessing values from dynamic or multiple form fields upon submission in React?

In my form, users have the ability to add Textfields in order to include additional items. However, I am facing a challenge when it comes to retrieving these items from the form upon submission. The Textfields are dynamically created by a component functi ...

jqote coming back as nothing

I am currently in the process of updating an HTML table with fresh data obtained through json and utilizing jQote. My jQote template is quite straightforward: <script type="text/html" id="template"> <![CDATA[ <tr> & ...

Typescript is throwing an error when trying to use MUI-base componentType props within a custom component that is nested within another component

I need help customizing the InputUnstyled component from MUI-base. Everything works fine during runtime, but I am encountering a Typescript error when trying to access the maxLength attribute within componentProps for my custom input created with InputUnst ...

Integrating my HTML-CSS layout into a Ruby on Rails framework

After creating a web page template using a combination of HTML, Bootstrap, and CSS, I am now looking to see it in action on my Rails application. I need guidance on how to accomplish this step by step. Can anyone provide assistance on what steps need to ...

Show the center portion of an image without needing to know the size, both horizontally and vertically aligned

I have experimented with various methods like table-cell, but none of them have been successful. All I am seeking is an <img src="" /> within an <a> tag. The <a> element has specific width, height, and overflow:hidden properties set. H ...