I want to change the class of a td tag given the td tag's id:

<td id="td_id" class="change_me"> ...

I want to be able to do this while inside the click event of some other dom object. How do I grab the td's id and change its class?

Solution 1

Using jQuery You can set the class (regardless of what it was) by using .attr(), like this:

$("#td_id").attr('class', 'newClass');

If you want to add a class, use .addclass() instead, like this:


Or a short way to swap classes using .toggleClass():

$("#td_id").toggleClass('change_me newClass');

Here's the full list of jQuery methods specifically for the class attribute.

Solution 2

I think you're looking for this:


Solution 3

I think he wants to replace a class name.

Something like this would work:


from http://monstertut.com/2012/06/use-jquery-to-change-css-class/

Solution 4

You can do This : $("#td_id").removeClass('Old_class'); $("#td_id").addClass('New_class'); Or You can do This


Solution 5

So you want to change it WHEN it's clicked...let me go through the whole process. Let's assume that your "External DOM Object" is an input, like a select:

Let's start with this HTML:

    <select id="test">

  <table id="theTable">
    <tr><td id="cellToChange">Bob</td><td>Sam</td></tr>

Some very basic CSS:

#theTable td {
    border:1px solid #555;
.activeCell {

And set up a jQuery event:

function highlightCell(useVal){
  $("#theTable td").removeClass("activeCell")


Now, whenever you pick something from the select, it will automatically find a cell with the matching text, allowing you to subvert the whole id-based process. Of course, if you wanted to do it that way, you could easily modify the script to use IDs rather than values by saying


and make sure to add the ids appropriately. Hope this helps!

Solution 6

You can check out addClass or toggleClass

Solution 7


If you're saying that you're changing it from a nested element, you don't need the ID at all. You can do this instead:

$(this).closest('td').toggleClass('change_me some_other_class');
$(this).parents('td:first').toggleClass('change_me some_other_class');

Original answer:


Another option is:

$('#td_id').toggleClass('change_me some_other_class');

Solution 8

In the event that you already have a class and need to alternate between classes as oppose to add a class, you can chain toggle events:

$('li.multi').click(function(e) {

Solution 9

$('.btn').click(function() {


$('.btn').click(function() {

Solution 10

this method works well for me

$('#td_id').attr('class','new class');

Solution 11

change class using jquery

try this

$('#selector_id').attr('class','new class');

you can also set any attribute using this query

$('#selector_id').attr('Attribute Name','Attribute Value');

Solution 12

Here is the solution using jQuery:

  if($('#td_id').hasClass('change_me')) {

Solution 13

I better use the .prop to change the className and it worked perfectly:


for this line of code you just have to change the name of newClass, and of course the id of the element, in the next exemple : idelementinyourpage


By the way, when you want to search which style is applied to any element, you just click F12 on your browser when your page is shown, and then select in the tab of DOM Explorer, the element you want to see. In Styles you now can see what styles are applied to your element and from which class its reading.