Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a tag appear when you click a checkbox

Tags:

html

css

How to make this tag <h3> appear when a checkbox is clicked? Right now it is hidden.

<h3 class="bark">Bark Bark</h3>
<input type="checkbox" class="title">Hear a dog</input>

css:

.bark{ 
  visibility: hidden
}

input[type="checkbox"]:checked  {
visibility: visible  
}
like image 870
Deke Avatar asked Dec 16 '15 04:12

Deke


People also ask

How do I make a checkbox tag in HTML?

The <input type="checkbox"> defines a checkbox. The checkbox is shown as a square box that is ticked (checked) when activated. Checkboxes are used to let a user select one or more options of a limited number of choices. Tip: Always add the <label> tag for best accessibility practices!

How do you show and hide input field based on checkbox selection?

To show or hide the field, we are applying CSS through jQuery css() method. We are passing CSS display property. To hide the field, we set the display property to none and to show it we set the display property block. So you have seen how to show or hide input field depending upon a checkbox field.


3 Answers

.bark{ 
  visibility: hidden
}

input[type="checkbox"]:checked + h3.bark  {
visibility: visible  
}
<input type="checkbox" class="title">Hear a dog</input>
<h3 class="bark">Bark Bark</h3>
like image 84
yjs Avatar answered Sep 22 '22 11:09

yjs


Without using scripting language there is one trick:

.bark{ 
  visibility: hidden;
}

input[type="checkbox"]:checked + .bark {
   visibility: visible;
}
<input type="checkbox" class="title">Hear a dog</input>
<h3 class="bark">Bark Bark</h3>

You should put h3 after input. And use + sign to make h3 visible.

Working Fiddle

like image 40
ketan Avatar answered Sep 20 '22 11:09

ketan


function checkInput(cbox) {
      if (cbox.checked) {
          document.getElementById('check_box').style.visibility='visible';
        }
      else{
            document.getElementById('check_box').style.visibility='hidden';
       }
 }
#check_box{ 
  visibility: hidden;
}
<h3 id="check_box">Bark Bark</h3>
<input type="checkbox" class="title"  onclick="checkInput(this);">Hear a dog
like image 21
Bijal Avatar answered Sep 21 '22 11:09

Bijal