I've managed to create a functionality that allows for showing and hiding a div based on radio button selection. However, I am facing an issue where the main div grows infinitely when the "yes" option is clicked multiple times, and shrinks to nothing when the "no" option is clicked repeatedly.
My goal is to implement a boolean variable that will control the growth of the form only once when "yes" is clicked, and the shrinkage once when "no" is clicked. Below is an example code snippet:
toggle();
function toggle() {
var optionA = document.getElementById("yes");
var optionB = document.getElementById("no");
var question = document.getElementById("moreQuestions");
var form = document.getElementById("form");
var height = form.offsetHeight;
var newHeight = height + 200;
var oldHeight = height - 200;
if (optionA.checked) {
question.style.display = "";
form.style.height = newHeight + 'px';
} else if (optionB.checked) {
form.style.height = oldHeight + 'px';
question.style.display = "none";
} else {
question.style.display = "none";
}
}
.form {
width: 400px;
height: 400px;
position: relative;
margin: 5% auto;
background: rgba(255, 255, 240, 0.733);
padding: 0px;
overflow: hidden;
border-radius: 30px;
}
.radio {
margin-left: 18px;
margin-top: 3px;
font-weight: normal;
}
.select-box {
margin-left: 18px;
margin-top: 3px;
margin-bottom: 3px;
}
<div class="page"></div>
<div class="form" id="form">
<div class="radio"><label>Show More?</label><br>
<label><input id="yes" name="radio1" type="radio" value="yes" onclick="toggle()" required="" />Yes</label><br>
<label><input id="no" name="radio1" type="radio" value="no" onclick="toggle()" required="" />No</label><br><br>
</div>
<div id="moreQuestions">
<select class="select-box">
<option value="0">0</option>
<option value="0.5">0.5</option>
<option value="1">1</option>
<option value="1.5">1.5</option>
<option value="2">2</option>
<option value="2.5">2.5</option>
<option value="3">3</option>
<option value="3.5">3.5</option>
<option value="4">4</option>
<option value="4.5">4.5</option>
<option value="5">5</option>
</select><br><br>
</div>
</div>
<script>
</script>