Wednesday, February 21, 2018
Quick and easy way to hide and show items on a page with JavaScript
In this scenario, we want to hide/show salary questions IF the user is in certain states:
Step 1: The Form
Form Fields: I added an onClick event to the radio buttons which will kick off the SHOW/HIDE function:
<input type="radio" value="0" onclick="javascript:showHideSalary();" id="legalStates0" name="legalStates"> NO
<input type="radio" value="1" onclick="javascript:showHideSalary();" id="legalStates1" name="legalStates"> YES
Step 2: The script
Add this script to the top of the page. Here I am saying if the users selects YES to the legal states question, hide the salary div.
<script>
function showHideSalary() {
if (document.getElementById('legalStates0').checked){
document.getElementById('salaryDiv').style.display = 'block'; display it
}
else document.getElementById('salaryDiv').style.display = 'none'; don't display it
}
</script>
Step 3: The DIV you want to hide.
<div id="salaryDiv" style="display:none">
* add whatever you want to hide/display here *
</div>
Enjoy and let me know if you have any issues.
Location:
Atlanta, GA, USA
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment