Show or hide elements with checkboxes
One of the components of Scatterpress is a javascript library that works with Prototype to make online forms more pleasant to use. For example, adding selection-dependent content to a form requires showing or hiding a page element based on whether a checkbox or radio button is checked. Ordinarily, we’d just specify an onclick attribute for the checkbox which would set the display of the page element to block (or inline) if the box is checked, to none if it isn’t.
But the Scatterpress front-end tools are all about progressive enhancement, and whenever possible they follow this principle: Use classnames to describe behavior; add behavior based on classnames. It’s the same idea behind the Behaviour library; all we’ve done is abstracted some of the attachable behaviors into libraries, and standardized a bunch of classnames which will produce those behaviors.
For selection-dependent content, the classnames will be show_if_checked and hide_if_checked, where checked is the id of a checkable element, i.e. a checkbox or radio button. To implement the account field in Luke Wroblewski’s example, we’d write markup like this:
<div>
<input type="radio" id="has_account" name="has_account" value="1">
<label for="has_account">I have an existing account</label>
</div>
<div class="show_if_has_account">
<div><label for="account_number">Please provide your account number:</label></div>
<div><input type="text" size="15" id="account_number" name="account_number"></div>
<p>Your account number is the 15-digit number in the upper right corner of your bill</p>
</div>
<div>
<input type="radio" id="doesnt_have_account" name="has_account" value="0">
<label for="doesnt_have_account">I don't have an account yet</label>
</div>
We could also have a section with the classname show_if_doesnt_have_account that might contain information about signing up for an account. It’s important to note that it’s the id of the checkable element that’s specified, not the name.