Darth_Brooks
07-07-2007, 05:55 PM
I'm creating a simple calculator. For some reason, the
document.getElementById()
isn't retrieving the object properly, presumably because
of the DOM. I reduced my code quite a bit to make
the problem easier to identify.
Would someone be able to help me identify the best
practices for retrieving objects that are within the
document?
Here's a snippet of my code:
<html><head>
<style type="text/javascript">
<!--
var extension = document.getElementById('spanId').getElementById('formId');
function setValue() {
//function should set the value of text-box to "Answer here"
extension.getElementById('answerBox').value = "Answer here";
}
//-->
</style>
</head>
<body bgcolor="bisque" onload="setValue()">
<div style="background: black;"><font size="15" color="white">Javascript10.1.html</font></div>
<span id="spanId" style="position: absolute; left: 50%; top: 70;">
<form id="formId">
<table bordercolor="brown" border=".5" cellspacing="0" cellpadding="0" width="150">
<tr>
<td colspan="2" width="50%"><input type="text" id="num1" /> </td>
<td colspan="2" width="50%"><input type="text" id="num2" /> </td>
</tr>
<tr>
<td width="25%"> <input type="button" style="width: 100%;" value="+" onclick="add()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="-" onclick="sub()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="*" onclick="mult()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="/" onclick="div()" /> </td>
</tr>
<tr height="25">
<td width="75%" colspan="3" />
<td width="25%" style="background-color: burlywood;" align="right">
<input type="text" id="answerBox" style="width :100%"/>
<!-- This is the trouble-spot... -->
</td>
</tr>
</table>
</form>
</span>
</body>
</html>
I've been banging my head on this one and must be missing something elemental. Thanks!
document.getElementById()
isn't retrieving the object properly, presumably because
of the DOM. I reduced my code quite a bit to make
the problem easier to identify.
Would someone be able to help me identify the best
practices for retrieving objects that are within the
document?
Here's a snippet of my code:
<html><head>
<style type="text/javascript">
<!--
var extension = document.getElementById('spanId').getElementById('formId');
function setValue() {
//function should set the value of text-box to "Answer here"
extension.getElementById('answerBox').value = "Answer here";
}
//-->
</style>
</head>
<body bgcolor="bisque" onload="setValue()">
<div style="background: black;"><font size="15" color="white">Javascript10.1.html</font></div>
<span id="spanId" style="position: absolute; left: 50%; top: 70;">
<form id="formId">
<table bordercolor="brown" border=".5" cellspacing="0" cellpadding="0" width="150">
<tr>
<td colspan="2" width="50%"><input type="text" id="num1" /> </td>
<td colspan="2" width="50%"><input type="text" id="num2" /> </td>
</tr>
<tr>
<td width="25%"> <input type="button" style="width: 100%;" value="+" onclick="add()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="-" onclick="sub()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="*" onclick="mult()" /> </td>
<td width="25%"> <input type="button" style="width: 100%;" value="/" onclick="div()" /> </td>
</tr>
<tr height="25">
<td width="75%" colspan="3" />
<td width="25%" style="background-color: burlywood;" align="right">
<input type="text" id="answerBox" style="width :100%"/>
<!-- This is the trouble-spot... -->
</td>
</tr>
</table>
</form>
</span>
</body>
</html>
I've been banging my head on this one and must be missing something elemental. Thanks!