383

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?

Syscall
  • 19,327
  • 10
  • 37
  • 52
aeq
  • 10,377
  • 15
  • 45
  • 46

13 Answers13

780

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:

$("#td_id").addClass('newClass');

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.

Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Hey @Nick, add an event handler that finds the first td parent [`var $td = $(this).parents('td:first')`] then gets it's id [`var id = $td.attr('id');`] and also the class [`var class = $td.attr('class');`] because he wants a click element inside the TD to trigger it – Bob Fincheimer Aug 10 '10 at 19:56
  • @Bob - I don't follow, where are you getting that from, another question? This one says he has the ID of the element :) Also to get a parent you can use `.closest('td')` instead of `.parents('td:first')` :) – Nick Craver Aug 10 '10 at 19:58
  • 1
    his last sentence: `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?` - maybe i am reading it wrong, maybe he means use the td's id to change the class... – Bob Fincheimer Aug 10 '10 at 19:59
  • 1
    @Bob - Right...some *other* DOM object, you're *assuming* that object is inside the `` in question :) – Nick Craver Aug 10 '10 at 20:01
  • Note that the `change_me` part is optional if you use `toggleClass()` – sakisk Dec 16 '16 at 14:11
103

I think you're looking for this:

$('#td_id').removeClass('change_me').addClass('new_class');
Fosco
  • 38,138
  • 7
  • 87
  • 101
33

I think he wants to replace a class name.

Something like this would work:

$(document).ready(function(){
    $('.blue').removeClass('blue').addClass('green');
});

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

Nighto
  • 3,994
  • 3
  • 23
  • 28
jack
  • 341
  • 3
  • 2
9

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

$("#td_id").removeClass('Old_class').addClass('New_class');
DanC
  • 1,297
  • 4
  • 24
  • 42
Alireza Masali
  • 668
  • 1
  • 10
  • 19
7

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:

<body>
  <div>
    <select id="test">
      <option>Bob</option>
      <option>Sam</option>
      <option>Sue</option>
      <option>Jen</option>
    </select>
  </div>

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

Some very basic CSS:

​#theTable td {
    border:1px solid #555;
}
.activeCell {
    background-color:#F00;
}

And set up a jQuery event:

function highlightCell(useVal){
  $("#theTable td").removeClass("activeCell")
      .filter(":contains('"+useVal+"')").addClass("activeCell");
}

$(document).ready(function(){
    $("#test").change(function(e){highlightCell($(this).val())});
});

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

.filter("#"+useVal)

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

Trafalmadorian
  • 2,498
  • 2
  • 21
  • 13
6

You can check out addClass or toggleClass

Syscall
  • 19,327
  • 10
  • 37
  • 52
Karl Johan
  • 4,012
  • 1
  • 25
  • 36
5

EDIT:

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');
    //or
$(this).parents('td:first').toggleClass('change_me some_other_class');

Original answer:

$('#td_id').removeClass('change_me').addClass('some_other_class');

Another option is:

$('#td_id').toggleClass('change_me some_other_class');
user113716
  • 318,772
  • 63
  • 451
  • 440
  • I don't think the OP's inside the ``, though he might be, re-read carefully :) – Nick Craver Aug 10 '10 at 20:05
  • @Nick - Yeah, I added the update because I read it again and saw that OP was asking *"How do I grab the td's id and change..."*. This led me to believe that OP doesn't have a handle on the ID, which would make my original answer not very helpful. Given this, I'm assuming the element with the handler is nested. But it certainly is an assumption. – user113716 Aug 10 '10 at 20:09
2

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) {
    $(this).toggleClass('opened').toggleClass('multi-opened');
});
lentyai
  • 534
  • 1
  • 9
  • 22
2
$('.btn').click(function() {
    $('#td_id').removeClass();
    $('#td_id').addClass('newClass');
});

or

$('.btn').click(function() {
    $('#td_id').removeClass().addClass('newClass');
});
Prabhu
  • 46
  • 1
  • 4
1

this method works well for me

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

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');
Love Kumar
  • 1,056
  • 9
  • 10
1

Here is the solution using jQuery:

$(document).ready(function(){
  if($('#td_id').hasClass('change_me')) {
    $('#td_id').removeClass('change_me').addClass('something_else');
  }
});
Rashed Rahat
  • 2,357
  • 2
  • 18
  • 38
-1

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

$(#td_id).prop('className','newClass');

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

$(#idelementinyourpage).prop('className','newClass');

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.