javascript – Is it possible to shorten this code to one line?-ThrowExceptions

Exception or error:

Is it possible to shorten this code to one line?

   window.addEventListener('load',function(){
      document.querySelector(".preloader-circle").classList.add('loaded');
      document.querySelector(".preloader").classList.add('loaded');
   });
How to solve:
window.addEventListener("load", function() {
  document.querySelectorAll("[class^=preloader]").forEach(el => {
    el.classList.add("loaded")
  });
});

This will add the loaded class to all the elements with selector class with prefix preloader

Reference for selector – https://www.w3.org/TR/selectors/#attribute-substrings

###

Do you mean shorten the code inside the function?

document.querySelectorAll(".preloader-circle, .preloader").forEach(x=>x.classList.add('loaded'));

###

function makeAnyCodeAOneLiner(code) {
   console.log(`You're welcome!`);
   return code.split('\n').join('')
}

makeAnyCodeAOneLiner(`window.addEventListener('load',function(){
      document.querySelector(".preloader-circle").classList.add('loaded');
      document.querySelector(".preloader").classList.add('loaded');
   });`)

Leave a Reply

Your email address will not be published. Required fields are marked *