Accessing event object

  • Thread starter Robert Mark Bram
  • Start date
R

Robert Mark Bram

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
defined".

Any help would be most appreciated!!

Thanks.

Rob
:)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top