Accessing event object

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Howdy All!

I have a problem. I am trying to provide code entirely within JavaScript,
including event handlers. To this end, I have provided JavaScript that sets
the onmouseover handler for a div element. The problem is that code within
JavaScript doesn't know what the event object is - despite the fact that
reference to "event" in a standard HTML handler is recognised.

The code below demonstrates.

A mouseover on the first div works perfectly - we see a dialog with "Event
type: mouseover".
A mouseover on the second div fails - an error comes up: "event is not

Any help would be most appreciated!!



  <script type="text/javascript" language="JavaScript">
   function buildBody() {
    var bodyDiv = document.createElement ("div");
    bodyDiv.onmouseover = function show(){alert ("event type: " +
    var text = document.createTextNode ("Here is the second div!");
    bodyDiv.appendChild (text);
    document.getElementById ("bodyElement").appendChild (bodyDiv);
   } // end buildBody function
 <body id="bodyElement" onLoad="buildBody();">
 <div onMouseOver="alert ('event type: ' + event.type);"><br>Here is the
first div!<br></div>

Site Timeline