Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  117] [ 7]  / answers: 1 / hits: 16415  / 12 Years ago, sun, july 15, 2012, 12:00:00

I need to remove the specific class attribute from all the element in the page. That is we need to remove the particular class from all the elements of the current html page. We only have the class name as the reference (which we need to remove from that page) and nothing else.



Please note that i cannot use anything as reference like id or another class and also i cannot use any JavaScript libraries for this also...



Any suggestion on this would be greatly appreciated.



Also note that the class we need to remove will not be there more than twice.


More From » javascript

 Answers
1
function removeClass(className) {
// convert the result to an Array object
var els = Array.prototype.slice.call(
document.getElementsByClassName(className)
);
for (var i = 0, l = els.length; i < l; i++) {
var el = els[i];
el.className = el.className.replace(
new RegExp('(^|\s+)' + className + '(\s+|$)', 'g'),
'$1'
);
}
}


and just use it as :



removeClass('desired-class-name');


UPDATE : see the live demo : http://jsbin.com/eyuxur


[#84244] Friday, July 13, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leighm

Total Points: 423
Total Questions: 101
Total Answers: 112

Location: Turkmenistan
Member since Sat, Apr 16, 2022
2 Years ago
;