user3191903
user3191903

Reputation: 245

CSS Change button image on click

I have two HTML image buttons (both are grey color), and whenever user clicks on any button I have to change grey color image to color image (I have 2 color images) and make other button grey. So user can know the active button.

How can I achieve this in CSS?

Upvotes: 0

Views: 1528

Answers (2)

Alfonso Jiménez
Alfonso Jiménez

Reputation: 1245

Add .button class (for example) to the buttons then use this 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">

Then with CSS you set the colors. You can have as many buttons as you want. See the fiddle below to check how it works.

Upvotes: 4

insomnium_
insomnium_

Reputation: 1820

I suppose you want to use something like:

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

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

Upvotes: 0

Related Questions