Reputation: 13
I have gender radio button for "male" and "female" options. I have also 2 divs: first for a "husband" input (to get if husband is alive or not) and the other one for "wife" input (to get if wife is alive or not).
I want to hide the husband input and show wife input when radio male is checked and I want to hide the wife input and show husband input when radio female is checked
I've tried to get the value of the radio selected but it didn't work. also tried to handle it by .checked operator that didn't word also. can you please tell what am I missing?
here is the code:
function toggle_visibility() {
var s = document.getElementById("divhus");
var c = document.getElementById("divwife");
var x = document.getElementById("gender").value;
if (x == "m") {
c.style.display = "block";
s.style.display = "none";
} else if (x == "f") {
c.style.display = "none";
s.style.display = "block";
}
}
male <input id="gender" type="radio" name="gender" value="m" checked="checked" onclick="toggle_visibility()"> female <input id="gender" type="radio" name="gender" value="f">
<div id="divwife">
wife: alive <input type="radio" name="wife" value="true"> dead <input id="hus1" type="radio" name="wife" value="false" checked="checked">
</div>
<div id="divhus" style="display:none">
husband: alive <input type="radio" name="husband" value="true"> dead <input id="hus1" type="radio" name="husband" value="false" checked="checked">
</div>
Thanks for support, and sorry for stupid question but I am a beginner.
Upvotes: 0
Views: 597
Reputation: 1932
You are facing two issues:
ad 1) just add the listener
ad 2) use a query-selector targeting the checked input e.g. below
function toggle_visibility() {
var s = document.getElementById("divhus");
var c = document.getElementById("divwife");
var x = document.querySelector('input[name = "gender"]:checked').value;
if (x == "m") {
c.style.display = "block";
s.style.display = "none";
}else if(x == "f"){
c.style.display = "none";
s.style.display = "block";
}
}
male <input id="gender_m" type="radio" name="gender" value="m" checked="checked" onclick="toggle_visibility()">
female <input id="gender_f" type="radio" name="gender" value="f" onclick="toggle_visibility()">
<div id="divwife" >
wife: alive <input type="radio" name="wife" value="true">
dead <input id="hus1" type="radio" name="wife" value="false" checked="checked">
</div>
<div id="divhus" style="display:none">
husband: alive <input type="radio" name="husband" value="true">
dead <input id="hus1" type="radio" name="husband" value="false" checked="checked">
</div>
Upvotes: 0
Reputation: 56754
Use a delegate change listener on an element that is an ancestor of both radio buttons:
const delegate = document.getElementById('malefemalecontainer') // get the delegate element (ancestor);
delegate.addEventListener( // add an event listener to the delegate
'change', // listening for change event bubbling up
function() { // function that handles the change when it occurs
const isMale = delegate.querySelector('input:checked').value === 'm'; // test if male is checked
document.getElementById('divwife').hidden = isMale; // if isMale, hide wife
document.getElementById('divhus').hidden = !isMale; // if not isMale, hide husband
}
);
<div id="malefemalecontainer">
<label>male <input type="radio" name="gender" value="m"></label>
<label>female <input type="radio" name="gender" value="f"></label>
</div>
<div id="divwife" hidden>
wife:
<label>alive <input type="radio" name="wife" value="true"></label>
<label>dead <input type="radio" name="wife" value="false" checked="checked"></label>
</div>
<div id="divhus" hidden>
husband:
<label>alive <input type="radio" name="husband" value="true"></label>
<label>dead <input type="radio" name="husband" value="false" checked="checked"></label>
</div>
Upvotes: 1
Reputation: 315
You can't ahave same id
for multiple elements in html. You need to call the function also when you click on other radio. Try to change like this:
<input type="radio" id="gender_m" onclick="toggle_function()"... >
<input type="radio" id="gender_f" onclick="toggle_function()"... >
And change the function like this:
function toggle_visibility() {
var s = document.getElementById("divhus");
var c = document.getElementById("divwife");
var x = document.getElementById("gender_m");
var y = document.getElementById("gender_f");
if (x.checked) {
c.style.display = "block";
s.style.display = "none";
}
if (y.checked) {
c.style.display = "none";
s.style.display = "block";
}
}
Upvotes: 0