0

I have some td elements (about 500) with multiple classes. How can I remove all but the first, knowing that this one may be different?

<td class"one tv-fox blue">value1</td>
<td class"two tv-cnn">value2</td>
<td class"grey tv-axn green">value3</td>
<td class"red tv-sky">value4</td>

Expected result

<td class"one">value1</td>
<td class"two">value2</td>
<td class"grey">value3</td>
<td class"red">value4</td>

Regards, Elio Fernandes

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Elio Fernandes
  • 1,335
  • 2
  • 14
  • 29
  • [This question](https://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery) shows you how to get a list of classes. You can use `removeClass` on all but the first result. – pritaeas Nov 09 '17 at 12:33

1 Answers1

2

You can do it like so:

var $tds = $('.myTable td');
$.each($tds, function(index, td) {
   var classNames = td.className.split(' ');
   classNames.shift()

   $(td).removeClass(classNames.join(' '));
});
adielhercules
  • 182
  • 1
  • 5