Search code examples
javascripthtmlxhtmlstylesdhtml

Change All Document Link Colors Javascript


Is there a way I can change all links on my page to a certain color at the same time? such as:

document.anchors.style.color = "red";

probably with an onclick function i'm thinking. No luck so far.


Solution

  • Maybe dynamically create a css for that.

    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if (styleElement.styleSheet) {
      styleElement.styleSheet.cssText = "a { color: red }";
    } else {
      styleElement.appendChild(document.createTextNode("a { color: red; }"));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);