jQuery, 1 line removeClass not working
-
03-12-2019 - |
Question
to start with I have this:
<body class="nojs">
I'm trying to remove the "nojs" class and replace with "js", so I can set CSS fallbacks properly. This is the code I'm using:
<script>$("body").removeClass("nojs").addClass("js");</script>
I'm connected to jQuery as other plugins are working fine, however this just won't work. Irrespective of whether I put it in the head or body tag.
Does anyone know what's wrong? Still does not work when in a (document).ready function.
Solution
Works fine for me:
<html>
<head>
<title>testing remove class</title>
<script src="http://code.jquery.com/jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body class="nojs">
<script>$("body").removeClass("nojs").addClass("js");</script>
</body>
</html>
As suggested above. If you don't use onReady then the DOM might not be ready in time for you to carry out your change.
OTHER TIPS
Try something like this:
$(document).ready(function() {
$('body').removeClass('nojs').addClass('js');
});
Are you getting any script errors?
Here's a jsfiddle to show this:
Basically, if you have this:
<div id="m">
<div id="a" class="nojs"></div>
</div>
and then do this:
alert($("#m").html())
$("#a").removeClass("nojs").addClass("js");
alert($("#m").html())
it works as expected. Can you try embedding the above example into your project and confirming it works?