1

In a container, two radio button inputs are placed. When the static button is checked, the containing div should be positioned static, when the absolute button is checked, the container should be positioned absolute. How to accomplish this JavaScript-free? Do I need additional markup?

div.container { border: 1px solid black; }

div.container input[id="static"]:checked + label {
    background-color: aqua;
}

div.container input[id="absolute"]:checked + label {
    background-color: green;
}
        <div class="container">
            <input id="static" type="radio" name="position" checked />
            <label for="static">Static</label>

            <input id="absolute" type="radio" name="position" />
            <label for="absolute">Absolute</label>
        </div>
TMOTTM
  • 2,663
  • 4
  • 21
  • 40

1 Answers1

0

Use onchange to change container style

function changePosition(pos){
var cont = document.getElementsByClassName("container")[0];
cont.style.position=pos;
}
div.container { border: 1px solid black; }

div.container input[id="static"]:checked + label {
    background-color: aqua;
}

div.container input[id="absolute"]:checked + label {
    background-color: green;
}
<div class="container">
  <input id="static" type="radio" name="position" checked onchange="changePosition('static')" />
   <label for="static">Static</label>

   <input id="absolute" type="radio" name="position" onchange="changePosition('absolute')"/>
   <label for="absolute">Absolute</label>
</div>
לבני מלכה
  • 14,372
  • 2
  • 15
  • 38