Replace jquery with pure javascript

Asked

Viewed 392 times

1

I have a jquery script to automatically remove a class from a div if browser javascript is enabled.

<script type="text/javascript">
        $("#scriptT").removeClass("noscript");
    </script>

But for this I need to import jquery (obviously), however this import causes conflicts with other things of the site that is done with primefaces (for those who do not know the primefaces already has the built-in jquery). And you can’t use the first faces jquery because on this specific page there are no primefaces components, which makes the jquery not imported (as I’m using facelets, the import links go all in the template, therefore all pages matter).

Anyway, can you replace this script with one in pure Javascript? remembering that it has to be automatic when initializing the site, without having to press any button.

The #scriptT id is in the div that I remove the noscript class

2 answers

2


So you won’t have to use the onload in the body. In itself JS, it will already load the method through the window.onload.

window.onload = removerClass();

function removerClass() {
   var element = document.getElementById("scriptT");
   element.classList.remove("noscript");
}

2

Diego’s solution is correct, but it doesn’t work in Internet Explorer 9 backward browsers (these browsers don’t support the property classList).

Just for compliment, in case someone needs it, the form below works on all browsers:

window.onload = removerClass();

function removerClass() {
   var element = document.getElementById("scriptT");
   var classes = element.className.split(" ");
   for(var x=0; x < classes.length; x++){
      if(classes[x] == "noscript") classes.splice(x, 1);
   }

   element.className = classes.join(" ");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.