Using window.event

R

RobG

I have a set of HTML elements that I add an onclick event to at
runtime. All it does is get the contents of an element using
innerHTML.

It all works fine in Firefox, but I can't get it to work in IE. No
doubt I have a really simple error.... help!

window.event returns an object, but
window.event.toElement returns null.

A broken (in IE) sample snippet is below:

<html><head><title>Arrgghhh</title>
<script type="text/javascript">
ie = document.all;
function showTxt(e) {
var d = (ie) ? window.event.toElement.innerHTML :
e.target.innerHTML;
alert(d);
}
function addClick(){
document.getElementById('rob').onclick = showTxt;
}

</script>
</head>
<body onload="addClick();">
<p id="rob">here is robs text</p>
</body>
</html>
 
M

Michael Winter

[snip]
window.event returns an object, but
window.event.toElement returns null.

It should. The toElement property, and its partner fromElement, are used
with the mouseout/over events to indicate where the mouse is going
to/coming from. The source of the event is given by srcElement.
<script type="text/javascript">
ie = document.all;
Umm...

function showTxt(e) {
var d = (ie) ? window.event.toElement.innerHTML :
e.target.innerHTML;

That isn't actually necessary.

var d = this.innerHTML;

should do. If you were to use the event object, the proper way would be
something like:

var d = (e ? e.target : window.event.srcElement).innerHTML;

[snip]

Mike
 
R

RobG

Michael Winter wrote:
[...]
That isn't actually necessary.

var d = this.innerHTML;

should do. If you were to use the event object, the proper way would be
something like:

var d = (e ? e.target : window.event.srcElement).innerHTML;

Thanks Michael, your first hint did the trick. I also appreciate your
abbreviated MS DOM test. I'd seen a similar version which would be (in
this case):

var d = (window.event ? window.event.srcElement:e.target).innerHTML;

But I think yours is better as it is more precise - it tests exactly
what I'm about to use - but I expect the result would be about as
consistent.

Rob.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top