Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Object Error... Why????

View previous topic View next topic Go down 
AuthorMessage
Deserve
Member
Member
Deserve

Posts : 7
Join date : 2011-05-11

Object Error... Why???? Empty
PostSubject: Object Error... Why????   Object Error... Why???? EmptyTue 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;
}
Back to top Go down
Impossible
Member
Member
Impossible

Posts : 2
Join date : 2011-05-11

Object Error... Why???? Empty
PostSubject: Re: Object Error... Why????   Object Error... Why???? EmptyTue Jun 21, 2011 6:57 pm

I don't see where you define addEvent.
Back to top Go down
Deserve
Member
Member
Deserve

Posts : 7
Join date : 2011-05-11

Object Error... Why???? Empty
PostSubject: Re: Object Error... Why????   Object Error... Why???? EmptyTue 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;
      }
  }
}
Back to top Go down
Skilletrockz
Super Moderator
Super Moderator
Skilletrockz

Posts : 45
Join date : 2011-05-11

Object Error... Why???? Empty
PostSubject: Re: Object Error... Why????   Object Error... Why???? EmptyTue 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.
Back to top Go down
Deserve
Member
Member
Deserve

Posts : 7
Join date : 2011-05-11

Object Error... Why???? Empty
PostSubject: Re: Object Error... Why????   Object Error... Why???? EmptyTue 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.
Back to top Go down
Sponsored content




Object Error... Why???? Empty
PostSubject: Re: Object Error... Why????   Object Error... Why???? Empty

Back to top Go down
 

Object Error... Why????

View previous topic View next topic Back to top 
Page 1 of 1

 Similar topics

-
» Error in rails gem installation
» recordset error in loop
» w3c validation - please help correcting the error
» Python error handling
» insane c error on seemingly correct code

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Object Error... Why???? Edit-trash Useless :: Trash-