V
V
Vasily Sakovich2016-11-27 20:54:20
JavaScript
Vasily Sakovich, 2016-11-27 20:54:20

Why are css classes not being removed in the className method in ie 9-11?

There is such a function:

function toggleClass(elem,cls,flag){
        
      var cls1=elem.className;
      
      cls1=cls1.split(' ');
      
      var i=cls1.indexOf(cls);
      
      if ( (i >= 0 && flag > 0) || (i <0 && flag < 0)){
          return;
      }
           
      if(flag < 0){
          cls1.splice(i,1);
      }
      
      if(flag >= 0){
          cls1.push(cls);
      }
      
      cls1=cls1.join(' ');

      elem.className=cls1;
  }

the result of its repeated call in ie is this:
f35b95fd2e0b496eb40b060df1746ecd.jpg
in other browsers everything is OK.
Thanks in advance for help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton fon Faust, 2016-11-29
@wasa20081980

Here is the description of the method javascript.ru/Array/splice
Here is the line with the error: cls1.splice(i,1);
Here's a hint: array indexes start at zero.
That's why it doesn't delete.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question