0

I need to update the class below dynamically using Jquery.

<tr id='myID' class="size colour">  <td></td> </tr>

I tried to use below:

$("#myID").toggleClass();

It doesn't seem to work since my class name is "size colour". Since the class name is dyanmic I could not use removeClass. Could someone help me please.

Thank you.

Nil Pun
  • 17,035
  • 39
  • 172
  • 294

4 Answers4

0

You need to use JqueryUI to toggle classes with animation.

Doc and demos here : http://jqueryui.com/demos/toggleClass/

sdespont
  • 13,915
  • 9
  • 56
  • 97
0

You might need to pass class name as parame i.e; .toggleClass( className )

Otherwise it is working, see here. Inspect element to see result.

http://www.jqapi.com/#p=toggleClass

Riz
  • 9,703
  • 8
  • 38
  • 54
0

Your classname is not size colour but your element has two classes, size and colour - these are independent of each other.

toggleClass() without any argument removes any currently present classes, so in your case

$("#myID").toggleClass();

would remove both classes, size and colour. On the second call all these classes will be added again.

If this is what you intended it to do, then it should work fine and the error is elsewhere. Else you have to clarify what exactly you want to achieve.

Christoph
  • 50,121
  • 21
  • 99
  • 128
-1

not sure if u want to remove class or add another class there...... but this might help...

// to remove class
$("#myID").attr('class','');  
output: <tr id='myID' class="">

//to add something to your exsiting class
var classname=$("#myID").attr('class');
$("#myID").attr('class',classname +' yourclassname');
output: <tr id='myID' class="size colour yourclassname">
bipen
  • 36,319
  • 9
  • 49
  • 62
  • even if this is what the OP wanted to do, you should be using `addClass()` and `removeClass()` to manipulate classes, and not `.attr()` – billyonecan Sep 20 '12 at 09:29
  • but here OP says classname is dynamic.. if u notice... "Since the class name is dyanmic I could not use removeClass." so i used attr...... and i don't thing removeClass() is gonna work for dynamic name @deifwud – bipen Sep 20 '12 at 09:33
  • That doesn't affect anything, your first line is effectively the same as using `$('#myID').removeClass()`, and the second `$('#myID').addClass('yourclassname')` – billyonecan Sep 20 '12 at 09:34
  • since both does the same thing.... why not use .attr ().. can u please explain... – bipen Sep 20 '12 at 09:37
  • [this question here](http://stackoverflow.com/questions/9090991/benefits-of-using-attr-over-addclass-in-jquery) has some good explanations in the answers – billyonecan Sep 20 '12 at 09:39
  • What you are doing is nonsense. You can always call `removeClass()` without parameters to delete all classes and `addClass()` to add a single class. The only usecase for `attr()` is if you want to override existing classes and add your own ones. In this case `attr('class',[classes])` is equivalent to `removeClass().addClass([classes])`. – Christoph Sep 20 '12 at 09:40