Jump to content

event handling not working in IE..


DanDaBeginner

Recommended Posts

I have this code just to practice on how to work with delegation.. and I was suprised that this code was not working in IE.. please somebody explain it to me why its not working..

 

this is place on the head tag of my .html

<script>
function e(y) {
	 var y = y || window.event;

	 var t = y.target || y.srcElement;

	 if (t.nodeName.toLowerCase() === 'a') {
	 	if (t.rel == 'alert') {
			alert(t.innerHTML);
			 return false;
		 }
	 }
}

add = function() {
	var t = document.getElementById("test");
	var a = document.createElement("a");
	a.rel = 'alert';
	var text = document.createTextNode("Added");
	var br = document.createElement("br");
	a.href="5";
	t.appendChild(br);
	a.appendChild(text);
	t.appendChild(a);
}

document.onclick = e;
</script>

 

in the body

<div id="test">
<a href="1" rel="alert">Hello</a><br />
<a href="2" rel="alert">Yikes!</a><br />
<a href="3" rel="alert">NP</a><br />
<a href="4" rel="alert">Tae!</a>
</div><br />
<br />
<a href="add" onclick="add(); return false;">Add</a>

Link to comment
Share on other sites

ok.. im trying to take an advantage on the bubbling phase of the event handler.. so I attached an onclick on the document object which the function e. I attached an onlclick event on document object to track every click on its childs, will be therefore handled by function e.

 

as you can see on function e, it checks whether the souce/ target element is an <a> tag then checks whether this <a> tag has a rel attribute with a value of 'alert'. if true then it will alert its innerHTML.

 

my problem is everything goes well on mozilla, bubbling phase was captured and it alerts for those <a> tags that has rel='alert' . but in IE nothing happens, and the worst is theres no errors popping up..

 

if you are interested i can send you the html file, 1.11 kb.

 

hope I explained it very well..

thanks again.

Link to comment
Share on other sites

solved.. in IE the document must be fully loaded before you can assign an handler to it.. (i guess) because my problem got fix when I added a setTimeout("set()",0). set() is the function that will assign an eventhandler to document.

 

solve button is missing so i can't solved this thread...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.