0

I would like to Change a HTML class with JavaScript

HTML

<i id="test" class="fa">

How can I do it really simple?

CredenTIAL
  • 58
  • 3

3 Answers3

0

Try this:

use .className to add and also get class name of particular element.

document.getElementById('test').className = "fa";
Dhaval Marthak
  • 17,246
  • 6
  • 46
  • 68
0

Aside from the className property, you can also use the much more elaborate classList property and its methods:

var ele = document.getElementById('test');
ele.classList.remove('fa');
ele.classList.add('ba');

You can also toggle() classes (with optional conditions!) and hence easily do without jQuery’s toggleClass() and its other class-related methods (like shown in the downvoted, accepted answer).

Community
  • 1
  • 1
dakab
  • 5,379
  • 9
  • 43
  • 67
-1

Try to use this in javascript:

 $("#test").removeClass("fa");
 $("#test").addClass("newClass");
Smudoo
  • 562
  • 3
  • 11