By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,366 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

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

P: 1
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.srcElement", 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="borderChanges">
<img src="path/to/img" alt="img description" />
<a href="link">Link Text</a>
<div>Some more Text</div>
</div>[/HTML]
This is the javascript that I am attaching to the div:

Expand|Select|Wrap|Line Numbers
  1. function linkHover (){
  2.   try {this.style.border = "2px solid green";}
  3.     catch (err){window.event.srcElement.style.border = "2px solid green";}
  4.   try {this.style.padding = "4px";}
  5.     catch (err){window.event.srcElement.style.padding = "4px";}
  6. }
  7. function linkOut (){
  8.   try {x=this.style.border = "1px solid black";}
  9.     catch (err){event.srcElement.style.border = "1px solid black";}
  10.   try {x=this.style.padding = "5px";}
  11.     catch (err){event.srcElement.style.padding = "5px";}
  12. }
Again, thank you to everybody.
Nov 10 '07 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,389
hi ...

welcome to TSDN ...

how do you attach the handlers to the div?

kind regards
Nov 11 '07 #2

acoder
Expert Mod 15k+
P: 16,027
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).
Nov 11 '07 #3

gits
Expert Mod 5K+
P: 5,389
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 ...
Nov 11 '07 #4

Dasty
Expert 100+
P: 101
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.
Nov 11 '07 #5

acoder
Expert Mod 15k+
P: 16,027
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.
Nov 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.