Using "this" and "window.event.srcElement" in IE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anEchteTrilingue
    New Member
    • Nov 2007
    • 1

    Using "this" and "window.event.srcElement" in IE

    Hi everybody. Thank you for reading my post.

    I am having trouble getting "this" to work in all versions of IE (it's fine in Firefox, opera, konqueror, etc). What I would like to do is add an event listener to an element to change its border on mouseover and mouseout. I don't want to use CSS to do this (long story).

    The problem is that "this" does not work at all in IE for me. I tried to do a try/catch statement and use "window.event.s rcElement", but this causes all of the child elements to fire the function as well, which is obviously not desireable (I don't want the children to have borders).

    So I have 2 questions:
    1. Do I have a syntax error that is preventing "this" from working in IE or does IE just not support it?
    2. How do I get the function to fire on the parent element only and not the children?


    This is what my html looks like:
    [HTML]<div class="borderCh anges">
    <img src="path/to/img" alt="img description" />
    <a href="link">Lin k Text</a>
    <div>Some more Text</div>
    </div>[/HTML]
    This is the javascript that I am attaching to the div:

    Code:
    function linkHover (){
      try {this.style.border = "2px solid green";}
        catch (err){window.event.srcElement.style.border = "2px solid green";}
      try {this.style.padding = "4px";}
        catch (err){window.event.srcElement.style.padding = "4px";}
    }
    function linkOut (){
      try {x=this.style.border = "1px solid black";}
        catch (err){event.srcElement.style.border = "1px solid black";}
      try {x=this.style.padding = "5px";}
        catch (err){event.srcElement.style.padding = "5px";}
    }
    Again, thank you to everybody.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    welcome to TSDN ...

    how do you attach the handlers to the div?

    kind regards

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Unfortunately, this is an important drawback of IE's event model. You can never know which element is currently handling the event. See link (Problems of the Microsoft model).

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        Originally posted by acoder
        Unfortunately, this is an important drawback of IE's event model. You can never know which element is currently handling the event. See link (Problems of the Microsoft model).
        hmmm ... good link :) ... so for the original problem in IE you have to ask for the srcElement and in case you get one that you don't want to handle you may climb up the dom and use the parentNode or something like this ...

        Comment

        • Dasty
          Recognized Expert New Member
          • Nov 2007
          • 101

          #5
          Originally posted by acoder
          Unfortunately, this is an important drawback of IE's event model. You can never know which element is currently handling the event. See link (Problems of the Microsoft model).
          This is easy avoidable. Just dont assign same function for 2 (more) nested elements if you want to handle event differently (makes sense to me).

          And to op: nobody can help you without knowing how you attach and to what elements you attack your event-handlers.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Dasty
            This is easy avoidable. Just dont assign same function for 2 (more) nested elements if you want to handle event differently (makes sense to me).
            Sometimes you can't avoid it and might need that information. I remember seeing some code that deals with (I think) this problem in IE. If I find it, I'll post the code or a link to it.

            Comment

            Working...