JSFiddle - React, Tailwind, and code Playground
by ashfame
HTML
<dl>
<dt>Login or Register</dt>
<dt class="complete b">Billing Address</dt>
<dt class="complete">Shipping Address</dt>
<dt class="x">Shipping Method</dt>
<dt>Payment Method</dt>
</dl>
JavaScript
function HasClassName(objElement, strClass) {
// if there is a class
if (objElement.className) {
var arrList = objElement.className.split(' ');
// get uppercase class for comparison purposes
var strClassUpper = strClass.toUpperCase();
for (var i = 0; i < arrList.length; i++) {
if (arrList[i].toUpperCase() == strClassUpper) {
return true;
}
}
}
// if we got here then the class name is not there
return false;
}
function AddClassName(objElement, strClass) {
// if we already have it, move on, we are done here
if (HasClassName(objElement, strClass)) return;
// we need to add it since its not there
// so, if there is a class, add a name to the class list else just specify it
if (objElement.className) {
var arrList = objElement.className.split(' ');
arrList[arrList.length] = 'current';
objElement.className = arrList.join(' ');
} else {
objElement.className = strClass;
}
}
function RemoveClassName(objElement, strClass) {
// if we already don't have it, move on, we are done here
if (!HasClassName(objElement, strClass)) return;
// we need to add it since its not there
// so, if there is a class, then only we need to look for and remove the culprit
var arrList = objElement.className.split(' ');
console.log(arrList);
for (var i in arrList) {
if (arrList[i].toUpperCase() == strClass.toUpperCase()) {
console.log('match');
arrList.splice(i, 1); // remove that element from array
}
}
console.log(arrList);
objElement.className = arrList.join(' ');
}
function a() {
var dts = document.getElementsByTagName('dt');
RemoveClassName(dts[1], 'b');
}
a();