Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Welby Obeng
Welby Obeng
20,340 Points

if you look at the code below I use javascript to show fields when another radio button is clicked. Is there way using h

function yesnoCheckcanwork() {
    if (document.getElementById('no_to_work').checked) {
        document.getElementById('notoworkexplain').style.display = 'block';
    }
    else
        document.getElementById('notoworkexplain').style.display = 'none';
}

function yesnoCheckcanfelony() {
    if (document.getElementById('yes_to_felony').checked) {
        document.getElementById('yestofelonyexplain').style.display = 'block';
    }
    else
        document.getElementById('yestofelonyexplain').style.display = 'none';
}
<label>Are you a U.S. citizen or otherwise authorized to work in the U.S. on an unrestricted basis?:</label>
<input type="radio" id="yes_to_work" value="yes_to_work" name="can_work"  onclick="javascript:yesnoCheckcanwork();" required="required"><label for="yes_to_work" class="light">Yes</label>
<input type="radio" id="no_to_work" value="no_to_work" name="can_work"    onclick="javascript:yesnoCheckcanwork();" required="required"><label for="no_to_work" class="light">No</label>

<div id="notoworkexplain" style="display:none">
    <label for="no_to_work_explain">Please explain:</label><textarea id="no_to_work_explain" name="no_to_work_explain"></textarea>
</div>

<label>Have you ever been convicted of a felony?:</label>
<input type="radio" id="yes_to_felony" value="yes_to_felony" name="felony" onclick="javascript:yesnoCheckcanfelony();" required="required"><label for="yes_to_felony" class="light">Yes</label>
<input type="radio" id="no_to_felony" value="no_to_felony"   name="felony" onclick="javascript:yesnoCheckcanfelony();" required="required""<label for="no_to_felony" class="light">No</label>

<div id="yestofelonyexplain" style="display:none">
    <label for="yes_to_felony_explain">Please provide date of conviction and fully describe the circumstances:</label><textarea id="yes_to_felony_explain" name="yes_to_felony_explain" ></textarea>
</div>

1 Answer

Welby Obeng
Welby Obeng
20,340 Points

All I needed to do is to set the element's required attribute to true or false.

                    function yesnoCheckcanwork() {
                        if (document.getElementById('no_to_work').checked) {
                            document.getElementById('notoworkexplain').style.display = 'block';
                            document.getElementById('no_to_work_explain').required = true;
                        } else {
                            document.getElementById('notoworkexplain').style.display = 'none';
                            document.getElementById('no_to_work_explain').required = false;
                        }
                    }

                    function yesnoCheckcanfelony() {
                        if (document.getElementById('yes_to_felony').checked) {
                            document.getElementById('yestofelonyexplain').style.display = 'block';
                            document.getElementById('yes_to_felony_explain').required = true;
                        } else {
                            document.getElementById('yestofelonyexplain').style.display = 'none';
                            document.getElementById('yes_to_felony_explain').required = false;
                        }
                    }