Updating image of a button using CSS when clicked

Looking for a solution to make HTML image buttons change color when clicked on? I have two grey image buttons and want them to turn into different colored images upon clicking, while also making the other button grey. This way, users can easily identify the active button.

Any suggestions on how this can be accomplished using CSS?

Answer №1

To give your buttons a .button class, simply add it to each button and implement the following jQuery code:

$( ".button" ).click(function() {
  $('.button' ).removeClass('active');
  $( this ).addClass('active');
});

<input type="button" class="button" id="button1" value="Button 1">
<input type="button" class="button" id="button2" value="Button 2">

You can then customize the colors using CSS. Feel free to include as many buttons as needed. Refer to the provided fiddle for visual demonstration.

Answer №2

If you're looking to change the button's background color, you can try the following code snippet:

function updateButtonColor(){
$("#button2").css("background-color","Green");
}

<input type="button" onClick="updateButtonColor()">
<input type="button" id="button2">

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

Generating a user interface (UI) by utilizing an EDMX file

What exactly is an EDMX file and what function does it serve? Is it possible to generate a UI screen using an EDMX file? If so, what are the steps involved in doing so? Thank you for your help ...

Utilizing BeautifulSoup for Web Scraping (handling missing values during the scrape)

I have been facing two challenges while trying to scrape data from a realtor website using BeautifulSoup. These issues have proven to be quite tricky: Challenges: After running my code snippet, I noticed that some date values are missing in the dataframe ...

Color remains unchanged for selected value in dropdown menu

I'm facing a challenge in changing the color of the selected value in the dropdown menu. Each value has a different color, but when I select one, it reverts back to black. .appt-status select option[value="0"] { color: #3EA47B; } .appt-status o ...

Struggling to set the background color for a div element

I am dealing with the following HTML and CSS: <div id="com_loaded"> <div id="com_loaded_userpic"><a href="#" class="tooltip"><img src="pic.jpg" class="img_poster" /><span>Username</span></ ...

Using React to Dynamically Display JSON Data as HTML

Struggling to incorporate HTML rendering from JSON data into my React component without relying on dangerouslySetInnerHTML. I want to include other React components within the rendered HTML, but facing challenges when trying to render multiple elements or ...

Placement issue with image in Bootstrap card on mobile devices

My Bootstrap card has an image in the second column that looks great on desktop, but not so much on mobile. <div class="mt-5 container card w-100"> <div class="row"> <div class="col"> <div class="card-body"> ...

Utilizing Bootstrap Containers for Improved Website Design Layouts

I'm a bit puzzled on the usage of Bootstrap's containers. Should I nest the entire body of my webpage within one container and then use rows for each section, or should I use individual containers for each section? For instance, do I do it like ...

In order to position content at the center of a <div> element

Just recently delving into the world of Bootstrap, I've hit a roadblock. Here's my current conundrum: <div class="container px-4 py-5" id="hanging-icons"> <h2 class="pb-2 border-bottom">Work Experie ...

What is the best method for clearing DOM variables and storage when a new innerHTML is loaded dynamically in a Single Page Application?

When I load Dynamic HTMLs with JS into a div on an index page, the content of the div gets updated dynamically based on user actions using the JQuery.load function. The loaded HTML includes JS files that are also added to the DOM and work as expected. How ...

The CSS linear gradients rainbow wheel is missing a few sections, only displaying part of its 12

I've almost got everything working, but I'm facing a challenge in displaying all 12 sections of the rainbow wheel. I know it's probably something very simple (Occam's razor). I've been following this amazing tutorial on CSS linear ...

Is there a way I can maintain the active state after clicking on a link?

Is there a way to maintain an active element state when navigating to another page after clicking a link? I am wondering if it is possible to use JavaScript to remove the active state from one link and apply it to the next one. I am facing some challenges ...

Is it possible to create a Footer with a 100% width in CSS?

I have a simple HTML page with all elements contained within a mainWrapper div and secondWrapper div. Everything is currently sized to 960px, including the pageHeader, pageContent, and pageFooter. However, I need to keep everything at 960px except for th ...

Is it possible to create and view HTML files in Objective C through user interaction?

I am currently working on developing an iOS app that enables users to create an unlimited number of HTML documents and save them within the app's documents folder. It's somewhat of a combination question, but how can I showcase a file that is sto ...

Creating Overlapping Sections Using Bulma CSS

I'm currently diving into the world of Bulma and aiming to create a simple page layout with the following structure: <header> <hero> <section> <footer> However, I'm encountering an issue where these elements are overlapp ...

Determine the dropdown list value by analyzing the final two variables in a textfield

In my textfield, car registration numbers are meant to be entered. These registrations are based on years in the format GT 74454 12, with the last two digits "12" representing the year 2012. I am looking for a script that can automatically detect the last ...

Form Style Components

I am currently using a form that contains material-ui components. Instead of relying on inline styles for the width property, I want to use css-in-js instead. I have previous experience with styled-components but there seems to be no form element available ...

Can anyone provide guidance on locating the parent of a pseudo element with Selenium WebDriver?

Is there a way to retrieve the parent web element of a pseudo element (if we can call it the parent) using JavaScript? I know that Selenium's methods for searching for web elements are not suitable for pseudo elements, but JavaScript does allow manipu ...

Create a design where the logo seems to be suspended from the navigation bar using bootstrap

My goal is to achieve a navigation bar similar to the one shown in this image: Using Bootstrap 3, the code below is what I have implemented for my navigation: <nav class="navbar navbar-default navbar-fixed-top"> <div class="container"> <di ...

Implementing a smooth transition effect, such as opacity or fade-in, on an image retrieved from an

I am struggling to achieve opacity with a transition on an image after clicking and changing the photo. I have created a basic slider that displays pictures from an array. How can I smoothly transition the opacity of the image from 0 to 1 over a duration ...

The parent node is returning a child element

Currently working on a website using Vue 3.0 and Element+ and encountering an inconsistency. There is a div displayed on the page (an array of objects) and the goal is to remove the object from the array when a button is clicked. The issue arises when som ...