DanDaBeginner Posted March 5, 2008 Share Posted March 5, 2008 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> Quote Link to comment Share on other sites More sharing options...
haku Posted March 5, 2008 Share Posted March 5, 2008 It doesn't work because there is a problem in the code. Quote Link to comment Share on other sites More sharing options...
DanDaBeginner Posted March 5, 2008 Author Share Posted March 5, 2008 thanks for your response.. for sure there was a problem.. can you atleast give me a hint on whats the problem? are you using a debugger or something? Quote Link to comment Share on other sites More sharing options...
haku Posted March 5, 2008 Share Posted March 5, 2008 You give me a hint on the problem, I'll try to figure out what the problem is! Sound good? What I'm trying to say is that you didn't give enough information in your first post to be able to help you. Quote Link to comment Share on other sites More sharing options...
DanDaBeginner Posted March 5, 2008 Author Share Posted March 5, 2008 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. Quote Link to comment Share on other sites More sharing options...
DanDaBeginner Posted March 6, 2008 Author Share Posted March 6, 2008 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... Quote Link to comment Share on other sites More sharing options...
haku Posted March 6, 2008 Share Posted March 6, 2008 Congrats! Sorry, I just sat down to look at this now and you've already solved it, so I couldn't help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.