<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Please, use Strict. Transitional shouldn't be used for new pages; we're well past the transitional phase now.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
Should really be sent by the server, rendering this pointless.
<style type="text/css">
<!--
/* ... */
-->
</style>
The comment delimiters are unnecessary, and have been so for a long time. Firefox will complain about them, too (although it shouldn't; the standard does make allowance for them).
<script language="javascript"
language is deprecated.
document.forms['form'].radios
... could well be undefined. Use the elements collection. Also, referring to the form in such an inflexible way is a bad idea.Abuse of <br>.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Animal Chooser</title>
<style type="text/css">
.animalHolder {
float: left;
width: 100px; /* width of your images, add a few pixels. */
}
.animalHolder label {
display: block;
}
</style>
<script type="text/javascript">
function getRadioValue(el) {
if(!el.length) return null;
for(var i = 0; i < el.length; ++i)
if(el[i].checked) return el[i].value;
return null;
}
function getElementsByClassName(root, tag, class) {
var op = [];
for(var i = 0, e = document.getElementsByTagName(tag); i < e.length; ++i)
if(e[i].className === class)
op.push(e[i]);
return op.length > 0 ? op : null;
}
function clearNode(node) {
while(node.hasChildNodes)
node.removeChild(node.firstChild);
}
function previewAnimals(frm) {
var f = frm.elements,
n = frm.elements['petName'],
s = getRadioValue(frm.elements['animal']),
p = getElementsByClassName(frm, "p", "preview")[0],
ps = p.getElementsByTagName("span"),
se = ps[0],
ne = ps[1],
ie = p.getElementsByTagName("img")[0],
op = getElementsByClassName(frm, "p", "code")[0].getElementsByTagName("textarea")[0],
isrc = f['animal'];
for(var i = 0; typeof isrc !== "string" && i < isrc.length; ++i)
if(isrc[i].checked)
isrc = isrc[i].parentNode.getElementsByTagName("img")[0].src;
ie.src = isrc;
clearNode(ne);
ne.appendChild(document.createTextNode(n));
clearNode(se);
se.appendChild(document.createTextNode(s));
op.value = '<div><p><img src="' + isrc + '"></p><p>This is my pet " + s + ", " + n + "!</p></div>";
return false;
}
</script>
</head>
<body>
<form action="" onsubmit="return previewAnimals(this);">
<p>
<label>
Pet name:
<input type="text" name="petName">
</label>
</p>
<p class="animalHolder">
<img src="cat.png">
<label>
Cat
<input type="radio" name="animal" value="cat">
</label>
</p>
<p class="animalHolder">
<img src="dog.png">
<label>
Dog
<input type="radio" name="animal" value="dog">
</label>
</p>
<p class="animalHolder">
<img src="civet.png">
<label>
Palm civet
<input type="radio" name="animal" value="palm civet">
</label>
</p>
<p class="preview">
<img src="dog.png">
This is my pet <span>dog</span>, <span>Fido</span>.
</p>
<p class="code">
Code:
<textarea rows="40" cols="80"></textarea>
</p>
<p>
<input type="submit" value="Go">
</p>
</form>
</body>
</html>
Untested. However, as djr33 said, it would be much better done on the server-side.
Bookmarks