3

I have buttons inside the table, inside table button is clicked want to change the background color of that button and other buttons on same color

I tried using jquery like below

$('#bt1').click(function() {
    $('#bt1').css('background', 'red');
});

but it works for one button only

<table id="abcd">
    <tr>
        <td><button class="cellGreen" id="bt1"><font>HOME</font></button></td>
        <td><button class="cellGreen" id="bt2"><font>PROJECT</font></button></td>
        <td><button class="cellGreen" id="bt3"><font>TEST</font></button></td>
        <td><button class="cellGreen" id="bt4"><font>Help</font></button></td>
    </tr>           
</table>

<button id="btdffd1"><font>Other Buttons</font></button>
<button id="btdffd1"><font> Buttons</font></button>

3 Answers 3

3

you have to go with its class cellGreen

$('.cellGreen').click(function() {
  $('.cellGreen').css('background', '');
  $(this).css('background', 'red');
});
Sign up to request clarification or add additional context in comments.

Comments

1

You probably need this:

Change your selector to a class .cellGreen and apply following script which will set other button's background to default and set red to the clicked one.

$('.cellGreen').click(function () {
     $(".cellGreen").css("background","");
     $(this).css('background', 'red');
});

Demo

Comments

0

Demo

For all buttons use $('td button') as the selector

$('#bt1').click(function() {
    $('button.cellGreen').css('background', 'red');
});

For click on all buttons, use:

$('button.cellGreen').click(function() {
    $('button.cellGreen').css("background","");
    $(this).css('background', 'red');
});

Note : Your last two buttons have duplicate Ids.

1 Comment

it will change background color of all the buttons, here i want to change the color of clicked button(inside the table) only

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.