Instead of individually calling $("#item").removeClass()
for every single class an element might have, is there a single function which can be called which removes all CSS classes from the given element?
Both jQuery and raw JavaScript will work.
This question is related to
javascript
jquery
dom
Just set the className
attribute of the real DOM element to ''
(nothing).
$('#item')[0].className = ''; // the real DOM element is at [0]
Edit: Other people have said that just calling removeClass
works - I tested this with the Google JQuery Playground: http://savedbythegoog.appspot.com/?id=ag5zYXZlZGJ5dGhlZ29vZ3ISCxIJU2F2ZWRDb2RlGIS61gEM ... and it works. So you can also do it this way:
$("#item").removeClass();
You can just try
$(document).ready(function() {
$('body').find('#item').removeClass();
});
If you have to access to that element without class name, for example you have to add a new class name, you can do that:
$(document).ready(function() {
$('body').find('#item').removeClass().addClass('class-name');
});
I use that function in my projet to remove and add class in a html builder. Good luck.
1.
// remove all items all class
const items = document.querySelectorAll('item');
for (let i = 0; i < items.length; i++) {
items[i].className = '';
}
2.
// only remove all class of first item
const item1 = document.querySelector('item');
item1.className = '';
$("#item").removeClass();
$("#item").removeClass("class1 ... classn");
Since not all versions of jQuery are created equal, you may run into the same issue I did which means calling $("#item").removeClass(); does not actually remove the class. (Probably a bug)
A more reliable method is to simply use raw JavaScript and remove the class attribute altogether.
document.getElementById("item").removeAttribute("class");
I had similar issue. In my case on disabled elements was applied that aspNetDisabled class and all disabled controls had wrong colors. So, I used jquery to remove this class on every element/control I wont and everything works and looks great now.
This is my code for removing aspNetDisabled class:
$(document).ready(function () {
$("span").removeClass("aspNetDisabled");
$("select").removeClass("aspNetDisabled");
$("input").removeClass("aspNetDisabled");
});
Hang on, doesn't removeClass() default to removing all classes if nothing specific is specified? So
$("#item").removeClass();
will do it on its own...
Of course.
$('#item')[0].className = '';
// or
document.getElementById('item').className = '';
$('#elm').removeAttr('class');
no longer class attr wil be present in "elm".
Let's use this example. Maybe you want the user of your website to know a field is valid or it needs attention by changing the background color of the field. If the user hits reset then your code should only reset the fields that have data and not bother to loop through every other field on your page.
This jQuery filter will remove the class "highlightCriteria" only for the input or select fields that have this class.
$form.find('input,select').filter(function () {
if((!!this.value) && (!!this.name)) {
$("#"+this.id).removeClass("highlightCriteria");
}
});
Heh, came searching for a similar answer. Then it hit me.
Remove Specific Classes
$('.class').removeClass('class');
Say if element has class="class another-class"
The shortest method
$('#item').removeAttr('class').attr('class', '');
try with removeClass
For instance:
var nameClass=document.getElementsByClassName("clase1");_x000D_
console.log("after", nameClass[0]);_x000D_
$(".clase1").removeClass();_x000D_
var nameClass=document.getElementsByClassName("clase1");_x000D_
console.log("before", nameClass[0]);
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div class="clase1">I am Div with class="clase1"</div>
_x000D_
Source: Stackoverflow.com