0

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?

2
  • I think you need javascript and optional css to do this Commented Apr 26, 2014 at 5:58
  • You'll need javascript for that. Commented Apr 26, 2014 at 5:59

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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">

3 Comments

It's more complex than that, he/she wants two buttons that behave like the radio inputs.
what if I have more than 10 buttons, do I have to write it for 10 times ? exactly @vikki
OK, got it. See @Alfonso Jiménez answer, he posted it before I properly edited my code to fit your request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.