Now for a bonus. Let’s tidy up the html code, so that we can make further progress with the script.
HTML attributes must not have spaces on either side of the equals sign.
Labels are used to enable visitors to more easily select form elements. When someone clicks on the text for “Yes” the label should activate the yes radio button.
Labels should not be used to surround multiple form elements.
The W3C provide details about the label element, but other places have good information about it too, such as the [URL=“http://webdesign.about.com/od/htmltags/p/bltags_label.htm”]about.com label element page.
Suffice it to say, if the label and form element are separate, the for attribute and matching id attribute are required to link them together. if the form element is inside the label, they are implicitly related without needing the for or id attributes.
So which to use? Let’s compare the two.
The initial question........blah blah.
<label><input type="radio" name="initial_Q" value="Yes" />Yes</label>
<label><input type="radio" name="initial_Q" value="No" />No</label>
The initial question........blah blah.
<input type="radio" name="initial_Q" id="initial_Q_yes" value="Yes" />
<label for="initial_Q_yes">Yes</label>
<input type="radio" name="initial_Q" id="initial_Q_no" value="No" />
<label for="initial_Q_no">No</label>
The id attributes make it easier to target certain parts from javascript, but the html is tidier without the id attributes all over the place and the parts er want to reach are easily accessible via the forms collection.
I’ve taken the liberty of including the code in an XHTML template, with a form identified as “questions” so that we can easily affect things from javascript.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Test</title>
</head>
<body>
<form id="questions" action="process.php">
<table>
<tr>
<td>
The initial question........blah blah.
<label><input type="radio" name="initial_Q" value="Yes" />Yes</label>
<label><input type="radio" name="initial_Q" value="No" />No</label>
</td>
</tr>
<tr id="message1">
<td>
<p>
Initial Question part a....?
<label><input type="radio" name="initial_a" value="Yes" />Yes</label>
<label><input type="radio" name="initial_a" value="No" />No</label>
</p>
<p>
Initial Question part b....?
<label><input type="radio" name="initial_b" value="Yes" />Yes</label>
<label><input type="radio" name="initial_b" value="No" />No</label>
</p>
</td>
</tr>
</table>
</form>
<script type="text/javascript" src="script.js">
</script>
</body>
</html>
Now we can focus properly on the javascript.
(function () {
function display(id) {
document.getElementById(id).style.display = '';
}
function hide(id) {
document.getElementById(id).style.display = 'none';
}
var frm = document.getElementById('questions');
var qyes = frm.elements.initial_Q[0];
var qno = frm.elements.initial_Q[1];
qyes.onclick = function () {
display('message1');
};
qno.onclick = function () {
hide('message1');
};
qno.click();
qno.onclick();
})();
The last two statements ensure that the no option is selected, and that the event for the no option is fired, ensuring that the section remains hidden when the page loads.
And as a final touch, the whole lot is wrapped in a self-executing anonymous function, which ensures that the environment doesn’t become polluted with functions and variables, helping to keep things nice and clean.