1

I'm setting up checkbox in a side navigation fashion in my html pages, I want the check box to display content upon selection. Example: Choose checkbox 1,2 display content only of that, No checkbox selected display all content.. How can i do this without Jquery/javascript. I want my webpage to maintain same functionality even if Javascript in browser is disabled. Can i do this by including w3.css as these styles are responsive without involving bootstrap

1

2 Answers 2

2

Below code has id for different checkbox and for there contents , onclick of any checkboxthat content will be display:

#content {
  display: none;
}

#show:checked ~ #content {
  display: block;
 }
  
#content1 {
  display: none;
}

#show1:checked ~ #content1 {
  display: block;
  }
<input type="checkbox" id="show">
<label for="show">check1</label>|
<input type="checkbox"id="show1">
<label for="show1">check2</label>


<span id="content">jvdkvdkvjdl</span>

 

<span id="content1">dkjvvhjvhkjdklhdlvk</span>

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

10 Comments

Although you have given input type="checkbox" why doesn't it show the checkbox to tick or untick?
Because i have given display:none to both checkboxes, See updated code i have change it .
Oh, Yes got that. Is it possible to sort the checkboxes alphabetically? If suppose it check1 and alphacheck 1. I want alphacheck1 to be displayed first?
Yes it is possible ,check this out .
No, I meant without JS
|
1

I would advise you to learn the ":checked" selector. Here is a simple example.

HTML

<label for="trigger">Toggle box</label>
<div>
    OTHER HTML CONTENT
</div>
<input id="trigger" type="checkbox">
<div class="box">
  SHOW / HIDE BOX
</div>

CSS

.box {
  display: none;
  background: #ccc;
  width: 200px;
  height: 100px;
}

#trigger {
  display: none;
}

#trigger:checked + .box {
  display: block;
}

As you can see the ":checked" selector can show or hide the DIV block. Here is an interesting article on this topic.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.