2

Consider the following HTML structure:

<input type=radio name=picker value=foo> <input type=radio name=picker value=bar> etc
<ul>
    <li class=foo>Foo #1</li>
    <li class=bar>Bar #1</li>
    <li class=foo>Foo #2</li>
    <li class=bar>Bar #2</li>
</ul>

I want to style those list items which match the currently-selected radio button. This works if all the possible radio button values are hard-coded:

input:checked[value=foo] ~ ul li[class~=foo] {
    background: blue;
    color: white;
}

input:checked[value=bar] ~ ul li[class~=bar] {
    background: blue;
    color: white;
}

Is there any way to say "input:checked" and then "li[class~=[the value of the currently-selected input]]" ? Assume that the radio buttons and list items are all dynamically generated.

If all else fails, I can dynamically generate the CSS too, but that seems unideal.

9
  • 1
    Did you try that stackoverflow.com/questions/1431726/… Commented Dec 2, 2019 at 13:28
  • @Awais Yes, this is more than can be done with simple sibling selectors. Commented Dec 2, 2019 at 13:34
  • Did that solve your problem Commented Dec 2, 2019 at 13:35
  • 1
    you cannot do this with CSS Commented Dec 2, 2019 at 13:36
  • if next sibling selector won't solve your problem (as in that link in the first comment) you should try with javascript Commented Dec 2, 2019 at 13:37

1 Answer 1

1

This seems to be a fundamental limitation of CSS (as of 2019 - if this changes in the future, someone can post a different answer). Since the input controls have to be script-generated anyway, it's simplest to script-generate the CSS as well.

Sign up to request clarification or add additional context in comments.

Comments

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.