I have been trying to figure this out and I can't seem to do so on my own. I have a function that works if I hardcode the div's name and use a single button. However, I would like to do this with about 10 different buttons and have them work with the same function instead of copying the function 10 times. Here is the working code:
document.getElementById("genderBtn").addEventListener("click", myFunction);
function myFunction() {
var x = document.getElementById("gender");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
/* Checkbox Lineup */
fieldset.group {
margin: 0;
padding: 0;
margin-bottom: 1.25em;
padding: .125em;
}
fieldset.group legend {
margin: 0;
padding: 0;
font-weight: bold;
margin-left: 20px;
font-size: 100%;
color: black;
}
ul.checkbox {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}
ul.checkbox li input {
margin-right: .25em;
}
ul.checkbox li {
border: 1px transparent solid;
display: inline-table;
width: 12em;
}
ul.checkbox li label {
margin-left: ;
}
ul.checkbox li:hover,
ul.checkbox li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}
/* Radio button lineup */
ul.radio {
margin: 0;
padding: 0;
margin-left: 20px;
list-style: none;
}
ul.radio li input {
margin-right: .25em;
}
ul.radio li {
border: 1px transparent solid;
display: inline-table;
width: 12em;
}
ul.radio li label {
margin-left: ;
}
ul.radio li:hover,
ul.radio li.focus {
background-color: lightyellow;
border: 1px gray solid;
width: 12em;
}
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
<fieldset class="group">
<legend>
<h1>Demographics <button type="button" id="genderBtn">Gender</button></h1>
</legend>
<ul class="checkbox">
<li><label for="name">Name: </label><input type="text" name="" id="name" value="" placeholder="default: client" /></li>
<li><label for="age">Age: </label><input type="text" name="" id="age" value="" /></li><br>
</ul>
<hr />
<div id="gender">
<b>Biological Sex</b>
<ul class="checkbox">
<li><input type="checkbox" name="gender[]" id="gender0" value="female" /><label for="gender0">Female</label></li>
<li><input type="checkbox" name="gender[]" id="gender1" value="male" /><label for="gender1">Male</label></li><br>
</ul>
<hr />
</div>
<b>Ethnic/Racial Information</b>
<ul class="checkbox">
<li><input type="checkbox" name="race[]" id="race0" value="Caucasian" /><label for="race0">Caucasian</label></li>
<li><input type="checkbox" name="race[]" id="race1" value="African-American" /><label for="race1">African-American</label></li>
<li><input type="checkbox" name="race[]" id="race2" value="Hispanic" /><label for="race2">Hispanic</label></li>
<li><input type="checkbox" name="race[]" id="race3" value="Arabic" /><label for="race3">Arabic</label></li>
<li><input type="checkbox" name="race[]" id="race4" value="Asian" /><label for="race4">Asian</label></li>
<li><input type="checkbox" name="race[]" id="race5" value="multi-racial" /><label for="race5">Multi-racial</label></li>
<li><label for="race6">Other:</label><input type="text" name="" id="race6" value="" /></li>
</ul>
</fieldset>
Now, when I try to change it to pass the div name as a variable, I can't seem to get it to work.
document.getElementById("someOtherBtn").addEventListener("click", myFunction(someOtherDiv));
function myFunction(theDivName) {
var x = document.getElementById("theDivName");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
Can someone please let me know what I'm doing wrong?
` and `` tags do not use and do not need a closing slash and never have in HTML. – Rob Feb 02 '20 at 01:03