how to change background color of my two td's using jQuery?

user3332446

this is my html code ..in the below code i am having two td's with same background color.so when i click on any of the two td's the background color must be changed using jquery.is it possible to do ????

<table id='main_table' style='width:1002px; border: 1px solid #ccc;'>
    <thead>
      <tr class='ab'>
             <td style='width:500px; background-color:#398bb7; color:white;' onclick=\"Consituteshow('"+cityid+"');\">By Constituency</td>
              <td  style='width:500px; background-color:#398bb7; color:white;' onclick=\"Partyshow('"+cityid+"');\">By Party</td>
      </tr>
    </thead>
</table>
Tushar Gupta - curioustushar

Use this keyword

$('#main_table tr.ab td').click(function () {
    $(this).css('background-color', '#398bb7');
});

If you want to change background color of both td

$('#main_table tr.ab td').click(function () {
    $(this).siblings().add(this).css('background-color', '#398bb7');
});

$('#main_table tr.ab td').click(function () {
    $(this).parent().find('td').css('background-color', '#398bb7');
});

Update after OP's comment

use .on()

As your elements are added dynamically so it is not present at the time DOM ready or page load.

So you have to use Event Delegation

Syntax

$( elements ).on( events, selector, data, handler );

js

$(function () {
    $(document).on('click', '#main_table td', function () {
        $("#main_table tr td").css("background-color", "#398bb7");
        $(this).css("background-color", "#FF00FF"); //Add your color
    });
});


Better use class

$(function () {
    $(document).on('click', '#main_table td', function () {
        $("#main_table tr td").removeClass('active');
        $(this).addClass('active');
    });
});

css

.active {
    background-color:green;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to change TD background color using jQuery

From Dev

How to catch the value in a td element with jquery and change another td elements background color based on the caught td's value?

From Dev

How to catch the value in a td element with jquery and change another td elements background color based on the caught td's value?

From Dev

How can I change the background color of table cell (td) using two buttons for different colors?

From Dev

How to change an item's background color smoothly using jquery?

From Dev

How can I change td background color depending on value in a table using JavaScript / jQuery?

From Dev

How to change color in first td in my table using class

From Dev

JavaScript: How to change the background color of each td?

From Dev

Change background color for a group of columns not first two columns using jquery

From Dev

change the background attribute of td using jquery

From Dev

How to change navbar element background color using jquery?

From Dev

How to change background color of datepicker form in Materialize design using jQuery?

From Dev

How to automatically change the background color on onclick, using JQuery and JSON API?

From Dev

Create table and change <td> background color using JavaScript

From Dev

can not change background-color using jQuery

From Dev

Change background color dynamically with animation using jQuery?

From Dev

Using jQuery to change background color on click

From Dev

How to show td's border over tr's background color

From Dev

How to show td's border over tr's background color

From Dev

How to change desired div's background color by entering hex color in input filed (with jQuery or without)?

From Dev

How to change desired div's background color by entering hex color in input filed (with jQuery or without)?

From Dev

How to change TextView's background color dynamically?

From Dev

How to change a cell's background color javascript

From Dev

How to change progress dialog's background color?

From Dev

How to change a background shape's color programmatically

From Dev

How to change background color of xml by using spinner

From Dev

How can I change the background color of a td when a link in the td is clicked?

From Dev

How to change the color of background or digits in date box using jquery mobile.?

From Dev

How to change background-color of the respective paragraph by clicking on prepended paragraph number using jQuery?

Related Related

  1. 1

    Unable to change TD background color using jQuery

  2. 2

    How to catch the value in a td element with jquery and change another td elements background color based on the caught td's value?

  3. 3

    How to catch the value in a td element with jquery and change another td elements background color based on the caught td's value?

  4. 4

    How can I change the background color of table cell (td) using two buttons for different colors?

  5. 5

    How to change an item's background color smoothly using jquery?

  6. 6

    How can I change td background color depending on value in a table using JavaScript / jQuery?

  7. 7

    How to change color in first td in my table using class

  8. 8

    JavaScript: How to change the background color of each td?

  9. 9

    Change background color for a group of columns not first two columns using jquery

  10. 10

    change the background attribute of td using jquery

  11. 11

    How to change navbar element background color using jquery?

  12. 12

    How to change background color of datepicker form in Materialize design using jQuery?

  13. 13

    How to automatically change the background color on onclick, using JQuery and JSON API?

  14. 14

    Create table and change <td> background color using JavaScript

  15. 15

    can not change background-color using jQuery

  16. 16

    Change background color dynamically with animation using jQuery?

  17. 17

    Using jQuery to change background color on click

  18. 18

    How to show td's border over tr's background color

  19. 19

    How to show td's border over tr's background color

  20. 20

    How to change desired div's background color by entering hex color in input filed (with jQuery or without)?

  21. 21

    How to change desired div's background color by entering hex color in input filed (with jQuery or without)?

  22. 22

    How to change TextView's background color dynamically?

  23. 23

    How to change a cell's background color javascript

  24. 24

    How to change progress dialog's background color?

  25. 25

    How to change a background shape's color programmatically

  26. 26

    How to change background color of xml by using spinner

  27. 27

    How can I change the background color of a td when a link in the td is clicked?

  28. 28

    How to change the color of background or digits in date box using jquery mobile.?

  29. 29

    How to change background-color of the respective paragraph by clicking on prepended paragraph number using jQuery?

HotTag

Archive