Deserve Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Deserve](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/adds/no_avatar.gif)
Posts : 7 Join date : 2011-05-11
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Object Error... Why???? Tue Jun 21, 2011 6:56 pm | |
| When I run this program is says I have an object error (not found?) in line 2, I went over the program which I copied from a book over and over and not finding anything wrong... Help!!! thanks HTML code here: - Code:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Making Sums</title> <script type="text/javascript" src="jsb-08-01.js"></script> <script type="text/javascript" src="jsb-global.js"></script> </head>
<body> <h1>Making Sums</h1> <form action="addit.php"> <p><input type="text" id="inputA" name="inputA" value="0" /></p> <p><input type="text" id="inputB" name="inputB" value="0" /></p> <p><input type="button" id="add" name="add" value="Add" /></p> <p><input type="text" id="output" name="output" /></p> </form> </body> </html>
Javascript code here: - Code:
-
// initialize when the page has loaded addEvent(window, 'load', initialize); // Here's where it says I have an error
function initialize() { // do this only if the browser can handle DOM methods if(document.getElementById) { // point to critical elements var oInputA = document.getElementById('inputA'); var oInputB = document.getElementById('inputB'); var oButton = document.getElementById('add'); var oOutput = document.getElementById('output'); // if they all exist... if(oInputA && oInputB && oButton && oOutput) { //apply behaviors addEvent(oButton, 'click', addIt); } } }
// add two input numbers & display results function addIt() { var value1 = document.getElementById("inputA").value; var value2 = document.getElementById("inputB").value; document.getElementById("output").value = value1 + value2; }
|
|
Impossible Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Impossible](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/adds/no_avatar.gif)
Posts : 2 Join date : 2011-05-11
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: Object Error... Why???? Tue Jun 21, 2011 6:57 pm | |
| I don't see where you define addEvent. |
|
Deserve Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Deserve](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/adds/no_avatar.gif)
Posts : 7 Join date : 2011-05-11
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: Object Error... Why???? Tue Jun 21, 2011 6:58 pm | |
| Here's the add event function that was missing: - Code:
-
/* jsb-global.js This file contains commonly-used functions for scripts accompanying The JavaScript Bible 7th Edition (Wiley, 2009) by Danny Goodman with Paul Novitski et al. */
// add an event to an element function addEvent(elem, evtType, func) { if (elem && typeof(elem) == "object") { // first try the W3C DOM method if (elem.addEventListener) { elem.addEventListener(evtType, func, false); } // otherwise use the 'traditional' technique else { elem["on" + evtType] = func; } } }
|
|
Skilletrockz Super Moderator
![Super Moderator Super Moderator](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/supermod.png)
![Skilletrockz](https://2img.net/u/1111/11/56/92/avatars/165-69.jpg)
Posts : 45 Join date : 2011-05-11
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: Object Error... Why???? Tue Jun 21, 2011 6:58 pm | |
| jsb-08-01.js is calling addEvent, which is in jsb-global.js, which loads later. Reverse their inclusion tags. |
|
Deserve Member
![Member Member](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/User%20ranks/member.png)
![Deserve](https://2img.net/h/i654.photobucket.com/albums/uu265/hoover78/adds/no_avatar.gif)
Posts : 7 Join date : 2011-05-11
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: Object Error... Why???? Tue Jun 21, 2011 6:59 pm | |
| skilletrockz... Makes sense... I wasn't thinking about that. Forgive a newbie... It definatley will be something I will think about next time though, thanks to you. |
|
Sponsored content
![Object Error... Why???? Empty](https://2img.net/i/fa/empty.gif) | Subject: Re: Object Error... Why???? ![Object Error... Why???? Empty](https://2img.net/i/empty.gif) | |
| |
|