Using window.event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RobG

    #1

    Using window.event

    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.to Element returns null.

    A broken (in IE) sample snippet is below:

    <html><head><ti tle>Arrgghhh</title>
    <script type="text/javascript">
    ie = document.all;
    function showTxt(e) {
    var d = (ie) ? window.event.to Element.innerHT ML :
    e.target.innerH TML;
    alert(d);
    }
    function addClick(){
    document.getEle mentById('rob') .onclick = showTxt;
    }

    </script>
    </head>
    <body onload="addClic k();">
    <p id="rob">here is robs text</p>
    </body>
    </html>
  • Michael Winter

    #2
    Re: Using window.event

    On Wed, 10 Nov 2004 05:58:04 GMT, RobG <rgqld@iinet.ne t.auau> wrote:

    [snip]
    [color=blue]
    > window.event returns an object, but
    > window.event.to Element returns null.[/color]

    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.
    [color=blue]
    > <script type="text/javascript">
    > ie = document.all;[/color]

    Umm...
    [color=blue]
    > function showTxt(e) {
    > var d = (ie) ? window.event.to Element.innerHT ML :
    > e.target.innerH TML;[/color]

    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.sr cElement).inner HTML;

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • RobG

      #3
      Re: Using window.event

      Michael Winter wrote:
      [...][color=blue]
      >
      > 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.sr cElement).inner HTML;
      >[/color]

      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.sr cElement:e.targ et).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.

      Comment

      Working...