26

I have the following HTML in my page.

<ul id="detailsList">
    <li>
        <input type="checkbox" id="showCheckbox" />
        <label for="showCheckbox">Show Details</label>
    </li>
    <li>content ...</li>
    <li>content ...</li>
    <li>content ...</li>
    <li>content ...</li>
    <li>content ...</li>
</ul>

I am unable to change this HTML. I have hidden all LI's with the exception of the first by using the following CSS

ul#detailsList li:nth-child(1n+2) {
    display:none;
}

So far so good. What I want to do now is to show those hidden LI's when the the checkbox is ticked, using pure CSS. My best attempt so far is

ul#detailsList li input#showCheckbox:checked + li {
    display:block;
}

Obviously this doesn't work, as the + li will only select LI's immediately after the checkbox (i.e. siblings), not siblings of the parent.

Is this even possible?

Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
Typhoon101
  • 2,063
  • 8
  • 32
  • 49

2 Answers2

15

You can use has():

ul#detailsList:has(> li > input#showCheckbox:checked) > li {
    display: block;
}

You cannot do that with CSS but

You can try using jQuery

$("#showCheckbox").click(function(){
    $(this).parent().siblings().show();
});
Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44
Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
2

You can't do it with CSS alone, you need to use javascript for this. As You need to catch the change event of the checkbox.

Sachin
  • 40,216
  • 7
  • 90
  • 102