Answers for "how to remove all Class(es) from a DOM, and then adds all Elements of an Array"

0

how to remove all Class(es) from a DOM, and then adds all Elements of an Array

function changeCSS(select, cssArray){
  //function that Removes all Class(es) from a 'queried DOM'
  //then adds back to that DOM element, all the entries from the 2nd parameter Array
  // this only works because elem is stored in Memory
  var myArray = [];
  
  var elem = document.getElementsByClassName(select);
  for(var i=0; i<elem.length; i++){
     myArray.push(elem[i]);     
  }  
  myArray.forEach((item, index)=>{
    console.log(myArray);
    myArray[index].removeAttribute('class');
    myArray[index].classList.add(select);
  });
  
  for(var ii=0; ii<elem.length; ii++){
    cssArray.forEach((item, index)=>{
      myArray[ii].classList.add(item);
    });
  }  
}
Posted by: Guest on September-17-2021

Code answers related to "how to remove all Class(es) from a DOM, and then adds all Elements of an Array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language