Your absolute most basic way of hiding/showing an element using JavaScript is by setting the visibility property of an element.
Given your example imagine you had the following form defined on your page:
<form id="form1">
<fieldset id="lenderInfo">
<legend>Primary Lender</legend>
<label for="lenderName">Name</label>
<input id="lenderName" type="text" />
<br />
<label for="lenderAddress">Address</label>
<input id="lenderAddress" type="text" />
</fieldset>
<a href="#" onclick="showElement('secondaryLenderInfo');">Add Lender</a>
<fieldset id="secondaryLenderInfo" style="visibility:hidden;">
<legend>Secondary Lender</legend>
<label for="secondaryLenderName">Name</label>
<input id="secondaryLenderName" type="text" />
<br />
<label for="secondaryLenderAddress">Address</label>
<input id="secondaryLenderAddress" type="text" />
</fieldset>
</form>
There are two things to note here:
- The second group of input fields are initially hidden using a little inline css.
- The "Add Lender" link is calling a JavaScript method which will do all the work for you. When you click that link it will dynamically set the visibility style of that element causing it to show up on the screen.
showElement()
takes an element id as a parameter and looks like this:
function showElement(strElem) {
var oElem = document.getElementById(strElem);
oElem.style.visibility = "visible";
return false;
}
Almost every JavaScript approach is going to be doing this under the hood, but I would recommend using a framework that hides the implementation details away from you. Take a look at JQuery, and JQuery UI in order to get a much more polished transition when hiding/showing your elements.