Checkbox to Show and Hide only for the near DIV

Posted by Holp on Stack Overflow See other posts from Stack Overflow or by Holp
Published on 2010-06-17T01:36:07Z Indexed on 2010/06/17 1:42 UTC
Read the original article Hit count: 266

Filed under:

Select all options... Then, when the user uncheck "B" and check it again, the "D" parents must be hidden. I have to do it without give then IDs.

<html>

<head>
    <title>Form</title>
    <style>
        * { font-family: Segoe UI, Verdana; font-size: 10pt; }
        #total { padding: 10px; position: fixed; top: 10px; left: 500px; width: 150px; height: 100px; }
        p { margin: 5px; }
        .grupo { padding: 5px 0 5px 0; }
    </style>
    <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
</head>

<body>

<div class="grupo">
<p class="pergunta">A) Lorem ipsum dolor sit amet, nulla nec tortor?</p>
<p><label><input type="radio" name="P-1" value="R-1-1" />Sim</label></p>
<p><label><input type="radio" name="P-1" value="R-1-2" />Não</label></p>
</div>

<div class="grupo">
<p class="pergunta"><label><input type="checkbox" name="P-2" value="R-2-3" />B) Donec libero risus, commodo vitae</label></p>

<div class="dependente">

<div class="grupo">

<p class="pergunta">C) Lorem ipsum dolor sit amet, nulla nec tortor?</p>
<p><label><input type="radio" name="P-3" value="R-3-1" />Morbi in orci</label></p>
<p><label><input type="radio" name="P-3" value="R-3-2" />Nulla purus lacus, pulvinar vel</label></p>
<p><label><input type="radio" name="P-3" value="R-3-3" />Aliquam ante</label></p>
<p><label><input type="radio" name="P-3" value="R-3-4" />Suspendisse scelerisque dui nec velit</label></p>
</div>

<div class="grupo">
<p class="pergunta"><label><input type="checkbox" name="P-4" value="R-4-5" />D) Donec libero risus, commodo vitae</label></p>

<div class="dependente">

<div class="grupo">
<p class="pergunta">E) Lorem ipsum dolor sit amet, nulla nec tortor?</p>
<p><label><input type="radio" name="P-5" value="R-5-1" />Morbi in orci</label></p>
<p><label><input type="radio" name="P-5" value="R-5-2" />Nulla purus lacus</label></p>
</div>

</div>
</div>

</div>
</div>

<div class="grupo">

<p class="pergunta">F) Lorem ipsum dolor sit amet, nulla nec tortor?</p>
<p><label><input type="radio" name="P-6" value="R-6-1" />Morbi in orci</label></p>
<p><label><input type="radio" name="P-6" value="R-6-2" />Nulla purus lacus, pulvinar vel</label></p>
<p><label><input type="radio" name="P-6" value="R-6-3" />Aliquam ante</label></p>
<p><label><input type="radio" name="P-6" value="R-6-4" />Suspendisse scelerisque dui nec velit</label></p>
</div>


<script type="text/javascript">
$('.dependente').hide();

$(':checkbox').click(function () {
    var checked = this.checked;
    $('.dependente:first',$(this).parents('div:first')).css('display',checked ? 'block':'none');
    $('.dependente input',$(this).parents('div:first')).attr('checked', false).change();
});
</script>

</body>

</html>

© Stack Overflow or respective owner

Related posts about jQuery