Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding a Check Box in HTML

I have these two checkboxes:

<label for="cell">3. Do you currently have a cell phone? (Y/N)</label>
<input type="checkbox" name="cell" name="cell" id="cell" data-on="Yes" data-off="No" />

<label for="blackBerry">4.If YES, is the cell phone a BlackBerry? (Y/N)</label>
<input type="checkbox" name="blackBerry" name="blackBerry" id= "blackBerry" data-on="Yes" data-off="No" />

How can I get the second question to display on the page, ONLY when the first one is selected as Yes?

Thanks in advance.

like image 386
UndefinedReference Avatar asked Nov 08 '12 22:11

UndefinedReference


1 Answers

You can do this using javascript

Javascript

​var elem = ​document.getElementById('cell');

​elem.addEventListener('click', function() {
      var divElem = document.getElementById('divPhone'); 
    if( this.checked){
        divElem.style.display = 'block'  ; 
    }
    else{
        divElem.style.display = 'none'  ;
    }
});

HTML

   <label for="cell">3. Do you currently have a cell phone? (Y/N)</label>
    <input type="checkbox" name="cell" name="cell"
           id="cell" data-on="Yes" data-off="No" />

    <div id="divPhone" class="hidden">
      <label for="blackBerry">4.If YES, is the cell phone a BlackBerry?
                                                          (Y/N)</label>
      <input type="checkbox" name="blackBerry" name="blackBerry" 
            id= "blackBerry" data-on="Yes" data-off="No" />
    </div>

​.hidden 
{
    display: none;
}​

Check Fiddle

​It is still more simple using jQuery..

​$('#cell').on('click', function() {
    if(this.checked){
       $('#divPhone').removeClass('hidden');
    }
    else{
       $('#divPhone').addClass('hidden');
    }        
});​

jQuery Fiddle

like image 77
Sushanth -- Avatar answered Sep 20 '22 06:09

Sushanth --