3

Supposing I have this HTML with multiple elements and class.

How can I remove without naming the elements from all the element the class classToRemove ?

Thanks.


<html>
<body class="classToRemove">
    <div class="classToRemove">
        <a class="classToRemove">Link</a>
    <div>
</body>
<html>
1
  • $('.classToRemove').removeClass('someClass');, if you want to remove the class itself $('.classToRemove').removeClass('classToRemove'); Commented Oct 2, 2015 at 19:49

2 Answers 2

2

with jQuery it's really easy $('.classToRemove').removeClass('classToRemove');

Sign up to request clarification or add additional context in comments.

Comments

0

Remove class using javascript

const removeClass = ($el, className) => {
  const _removeClass = function(el) {
    if (el.classList) {
      el.classList.remove(className);
    } else {
      el.className = el.className.replace(
        new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'),
        ' ',
      );
    }
  };
  if ($el.length === 1) {
    _removeClass($el);
  } else if ($el.length > 1) {
    $el.forEach(function(item) {
      _removeClass(item);
    });
  }
};

Comments