Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;
                        }
                    }