chris97ong
chris97ong

Reputation: 7060

How to get elements with certain style

So say I have this in my body:

<body>
<h1>Hello world!</h1>
<h2 style="color: Blue;">This is my webpage</h2>
<a style="color: Blue;" onClick="changeElem();">Welcome!</a><br>
<h3>Goodbye</h3>
</body>

I want to create function changeElem() such that it will change the content that is blue to black. So this is the result I want to get after using this function:

<h1>Hello world!</h1>
<h2 style="color: Black;">This is my webpage</h2>
<a style="color: Black;" onClick="changeElem();">Welcome!</a><br>
<h3>Goodbye</h3>

How can this be done?

Upvotes: 0

Views: 311

Answers (3)

radia
radia

Reputation: 1486

 function getElem(){
   var items = document.body.getElementsByTagName("*");
   for (var i = items.length; i--;) {
     style = window.getComputedStyle(items[i].innerHTML);
     color = style.getPropertyValue('color');
     if(color =="rgb(0,0,255)"){ 
        items[i].style.color="black";
       }
   }   
  }

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074425

You're much better off doing this with CSS, not inline styles.

<head>
<style>
/* By default, elements with class="some-class" are blue */
.some-class {
    color: blue;
}

/* But if body has the class "updated", they turn black */
body.updated .some-class {
    color: black;
}
</style>
<h1>Hello world!</h1>
<h2 class="some-class">This is my webpage</h2>
<a class="some-class" onClick="changeElem();">Welcome!</a><br>
<h3>Goodbye</h3>
</body>

...where changeElem is:

function changeElem() {
    document.body.className += " updated";
}

Live Example | Live Source


If you're dead set on using inline styles, which is not a good idea, you can still do it easily enough:

function changeElem() {
    var div, colorValue, list, index, element;

    // Figure out what this browser returns for `color: Blue`
    // (it might be "Blue", "blue", "rgb(0, 0, 255)",
    // "rgba(0, 0, 255, 0)", "#0000FF", "#0000ff",
    // or possibly others)
    div = document.createElement('div');
    document.body.appendChild(div);
    div.innerHTML = '<span style="color: Blue;"></span>';
    colorValue = div.firstChild.style.color;
    document.body.removeChild(div);

    // Get list of all elements that have any `style` attribute at all
    list = document.querySelectorAll('[style]');

    // Loop through looking for our target color
    for (index = 0; index < list.length; ++index) {
        element = list[index];
        if (element.style.color === colorValue) {
            element.style.color = "black";
        }
    }
}

Live Example | Live Source

Upvotes: 2

Thomas Junk
Thomas Junk

Reputation: 5676

I suggest working with Class Selectors.

<body onLoad="getElem();">
<h1>Hello world!</h1>
<h2 class="blue">This is my webpage</h2>
<a class="blue">Welcome!</a><br>
<h3>Goodbye</h3>
</body>    

Then you could easily select all Elements with a common class via document.querySelectorAll():

document.querySelectorAll(".blue")

for all Elements with the class blue (e.g.)

Then you could set the class of each element simply to black.

Upvotes: 0

Related Questions