1

I have a part of code in which the function removeClass must delete an element of array in case, if some of his elements coincides with input parameters.

But it does not work.

var obj = {
  className: 'open menu'
};

function removeClass(obj, cls) {
  var arr = obj.className.split(' ');

  for (var i = 0; i < arr.Length; i++) {
    if (cls == arr[i]) delete arr[i]
  }

  obj.className = arr.join(' ');

  return obj.className;
}

console.log(removeClass(obj, 'open'));
// desired output obj.className='menu'
// actual output 'open menu'

8
  • 2
    What doesn't work? You haven't provided any errors, or output or anything to give us any clear indication to what your problem is. Commented Dec 13, 2017 at 16:43
  • 2
    arr.length, NOT arr.Length... Commented Dec 13, 2017 at 16:44
  • 1
    Are you trying to be able to remove values from the className of a DOM element? If so there are easier ways to do this. Commented Dec 13, 2017 at 16:44
  • 1
    Why all this code for obj.classList.remove(cls)? Commented Dec 13, 2017 at 16:45
  • 1
    its arr.length not arr.Length Commented Dec 13, 2017 at 16:45

4 Answers 4

3

You can use Array.prototype.filter() method for this.

var obj = {
  className: 'open menu'
};

function removeClass(obj, cls) {
  var arr = obj.className.split(' ');

  obj.className = arr.filter(function(item) {
    return cls !== item;
  }).join(' ')

  return obj.className;
}

console.log(removeClass(obj, 'open'));

In your code, you have used arr.Length. Actual syntax is arr.length. But even if you use fix your code then, it will not delete the item instead put undefined on its place, then you have to handle extra white spaces. That's why I think above solution is good.

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

Comments

1

Try this:

    var obj = {
      className: 'open menu dogs cats'
    };

    function removeClass(obj, cls) {
      return (obj.className = obj.className.split(' ').filter(item => cls !== item).join(' '));
    }

    console.log(removeClass(obj, 'open'));
    console.log(removeClass(obj, 'dogs'));

But if you are trying to do this to a DOM element then use classList instead.

var el = document.getElementById("mine");

console.log(el.className);
el.classList.remove('open');
console.log(el.className);
el.classList.remove('dogs');
console.log(el.className);
<div id="mine" class="menu open dogs cats"></div>

Comments

0

Why just don't replace the string?

var obj = {
  className: 'open menu'
};

function removeClass(obj, cls) {
  return obj.className = obj.className.replace(cls, '').trim()
}

console.log(removeClass(obj, 'open')); // obj.className='menu'

Also while working with DOM there are already methods for dooing this

const b = document.querySelector('button')
b.addEventListener('click', () => {
  b.classList.remove('blue')
})
.red {
  color: red
}

.blue {
  color: blue
}
<button class="red blue">test</button>

Comments

0

With ES6 you can even make this really short

function removeClass(element, classToRemove) {
    return element.className.split(' ')
                  .filter(class => class !== classToRemove)
                  .join(' ');
}

If you want to modify the element itself, just reassign the return value to element.className

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.