0

Well, i have a code like this:

<div class="code">
  <div>
  <input id="yellow" type="radio" name="yellow" value="1">
  <label for="yellow"> yellow</label>
  </div>
  <div>
  <input id="red" type="radio" name="red" value="2">
  <label for="red"> red</label>
  </div>
  <div>
  <input id="black" type="radio" name="black" value="3">
  <label for="black"> black</label>
  </div>
  <div>
  <input id="green" type="radio" name="green" value="4">
  <label for="green"> green</label>
  </div>
</div>

Let's imagine, that user have selected one of these radiobuttons, for example, "black". I need to add some css to "black" label and all the labels, that goes before "black" - "red" and "yellow" labels.

It have to be something like this:

.code > div > label:checked ~ label:before { some css }

But not exactly, cause labels are not just neighbors, they are nested in their divs. What i have to add/change to this css selecting rule?

Alex Fatun
  • 51
  • 6
  • 3
    You cannot select backwards or upwards in CSS. Not possible. Only forward and downward from any point in a selector you've already reached. – connexo Dec 05 '21 at 21:39
  • What styling do you want to do? – A Haworth Dec 05 '21 at 21:42
  • well, is it possible with jquery? – Alex Fatun Dec 05 '21 at 21:43
  • i want to color these labels, for example – Alex Fatun Dec 05 '21 at 21:44
  • So, i solved it with jquery : function addColores() { var d = $("input:checked").closest("div").prevAll(); d.each(function() { $( this ).find('label').addClass("colored"); }); } – Alex Fatun Dec 06 '21 at 01:32
  • And i do not understand why this question is closed ;( It is not exactly the same as similar questions, and my solution with jquery, maybe, can help somebody – Alex Fatun Dec 06 '21 at 01:33
  • Alex, https://api.jquery.com/parent/ supports a selector so with jQuery you can get a child and then find a CSS selector above, this still should not be re-opened because no where have you tried some code and got a problem with the code your trying, and there are lots of questions about jQuery.parent(), and no backwards CSS selectors – Barkermn01 Dec 07 '21 at 12:50

0 Answers0