dev113
February 12, 2018, 4:33pm
1
I am trying to remove a class in Javascript (without jquery) but it doesnt seem to work. Is there something I am missing from the code?
function removeClass(selector, myClass) {
elements = document.querySelectorAll(selector);
for (var i=0; i<elements.length; i++) {
elements[i].classList.remove(myClass);
}
}
removeClass('top-header', 'bottom-footer', 'header-content');
mawburn
February 12, 2018, 4:39pm
2
You’re passing in 3 values to the function which only takes 2.
Your first value is not a selector, it is just a string which will try to resolve to an element ie: <top-header>
, which isn’t valid HTML. You will need to add #
for an id or .
for a class to the front of it. The .querySelectorAll()
method works just like $(selector)
in jQuery.
dev113
February 12, 2018, 4:49pm
3
I did try that first but didnt seem work either
Hi there dev113,
here is an example…
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
</head>
<body>
<div class="foo top-header"></div>
<div class="foo bottom-footer"></div>
<div class="foo header-content"></div>
<script>
(function(d) {
'use strict';
function removeClass( selector, myClass, myClass1, myClass2 ) {
var elements = d.querySelectorAll(selector);
for (var i = 0; i < elements.length; i ++ ) {
elements[i].classList.remove( myClass );
elements[i].classList.remove( myClass1 );
elements[i].classList.remove( myClass2 );
}
}
removeClass('.foo', 'top-header', 'bottom-footer', 'header-content');
}( document ));
</script>
</body>
</html>
coothead
dev113
February 12, 2018, 5:04pm
5
Is there a method to remove the css property and styles instead of the html div
The code does do that.
Try this with javascript enabled and disabled…
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<style media="screen">
.foo {
padding: 1em;
margin: 0.5em 0;
border: 0.062em solid #000;
text-align: center;
}
.top-header {
background-color: #fee;
}
.bottom-footer {
background-color: #eef;
}
.header-content {
background-color: #efe;
}
</style>
</head>
<body>
<div class="foo top-header">A</div>
<div class="foo bottom-footer">B</div>
<div class="foo header-content">C</div>
<script>
(function(d) {
'use strict';
function removeClass( selector, myClass, myClass1, myClass2 ) {
var elements = d.querySelectorAll(selector);
for (var i = 0; i < elements.length; i ++ ) {
elements[i].classList.remove( myClass );
elements[i].classList.remove( myClass1 );
elements[i].classList.remove( myClass2 );
}
}
removeClass('.foo', 'top-header', 'bottom-footer', 'header-content');
}( document ));
</script>
</body>
</html>
coothead
dev113
February 12, 2018, 5:27pm
7
coothead:
selector
I do not have a selector appended to the divs. Is it possible to just remove the classes?
Thanks
Can you show us the HTML elements from which you want to remove the class attributes?
coothead
dev113
February 12, 2018, 6:46pm
9
Just these
.top-header
.bottom-footer
.header-content
m3g4p0p
February 12, 2018, 6:56pm
10
Well if nothing else you can query these elements by the very classes you want to remove… e.g. using your removeClass()
function:
removeClass('.top-header', 'top-header')
removeClass('.bottom-footer', 'bottom-footer')
removeClass('.header-content', 'header-content')
system
Closed
May 15, 2018, 1:56am
11
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.