How To Style A Class By Attribute Value

  1. create your variable that stores all the objects that have been assaigned a class
  2. Create a For loop so it applies your condition to all objects
  3. Create an If statement to apply your condition to the objects with a given attribute that contains a certain value.
  4. By using .cssText command you can apply all your CSS rules as normal between the quotation marks.

How To Style A Class By Attribute Value Real Life Example:

var elements = document.getElementsByClassName('variable-item');
for (var i = 0; i < elements.length; i++) {
if (elements[i].getAttributeNode('data-wvstooltip').value == "None") {
elements[1].style.cssText = "color: red;";
} else {
elements[i].style.cssText = "color: gray; ";
}
}

Leave a Reply