0

What I want to achieve: All children with an opacity of 0.01 get an increment of 1 in their opacity values.

Problem: As you can see below, this is as far as I got. I cant figure out how to tell js to select all those children elements based on a computed value and add some style to them. I looped through them and got their indexes, as well as their computed values but that is where im stuck now.

HTML:

 <div id='a'>
    <span>How</span>
    <span>are</span>
    <span>you?</span>
 </div>

JS:

var elem = document.getElementById('a');
for (i = 0; i < 10; i++) {
    var x = window.getComputedStyle(elem.children[i], null).getPropertyValue('opacity');
    parseFloat(x)[i]
}

Question: How do I add some style to an array of elements that have a specific computed value of a property?

5
  • 1
    Why was this downvoted? Commented Oct 10, 2015 at 21:10
  • Possibly due to no reproducible code/html Commented Oct 10, 2015 at 21:14
  • @mplungjan I know but that person could have told me. Its was pretty rude. Ok I will add some more code Commented Oct 10, 2015 at 21:15
  • @mplungjan excuse me but is this fine now? Commented Oct 10, 2015 at 21:20
  • I guess you could add some styling so the code will actually do something. I am off to bed :) Commented Oct 10, 2015 at 21:21

1 Answer 1

1

You need to capture the return value parseFloat() if you want to do anything with it. You'll want to do something like this:

var elem = document.getElementById('a');
for (i = 0; i < elem.children.length; i++) {
    var x = window.getComputedStyle(elem.children[i], null).getPropertyValue('opacity');
    var opacity = parseFloat(x);
    if (opacity === 1.0) {
       elem.children[i].style.your_style = whatever;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

and also change 10 to elem.children.length otherwise it raise error if children count less than 10 :-)
@Grundy Thanks! For some reason I didn't bother to check that line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.