click() event for firefox
Part 1:
In my page below, I want to search out links that have a particular class name and make the parent element click on the first link inside that container.
This works in IE. In Firefox it repeatedly fires the "click" event, causing the onclick to fire infinitely. Can anyone see why?
<html> <head> <script type="text/javascript"> function clickableParents(vClass) { try { HTMLElement.prototype.click = function() { var evt = this.ownerDocument.createEvent('MouseEvents'); evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null); this.dispatchEvent(evt); } } catch (ex) {} var elms = document.getElementsByTagName("A"); for (var i=0; i<elms.length; i++) { if ((vClass>"") && (elms[i].className != vClass)) continue; elms[i].parentNode.onclick = function () { elm = this.childNodes[0]; while (elm.nodeType != 1) elm = elm.nextSibling; elm.click(); // ensures that the onclick fires } } } </script> </head>
<body onload="clickableParents('ok')"> <div style="border:1px solid red;width:300px;height:50px;text-align:center;vertical-align:center;" onclick="alert('onclick on the div');"> <a href="http://www.google.com/" target="google" onclick="return (confirm('Are you sure?'))" class="ok">Go to Google.com</a> </div> <div style="border:1px solid red;width:300px;height:50px;text-align:center;vertical-align:center;" onclick="alert('onclick on the div');"> <a href="http://www.yahoo.com/" target="yahoo" onclick="return (confirm('Are you sure?'))" class="ok">Go to Yahoo.com</a> </div> <div style="border:1px solid red;width:300px;height:50px;text-align:center;vertical-align:center;"> <a href="file:///doesnotexist.txt" onclick="return (confirm('Are you sure?'))">some other broken link</a> </div> </body> </html>
Part 2:
Also, I'm overwriting the onclick event on the wrapper div element.
Can someone show me a better way of achieving what I'm trying to do here, because I tried doing this by attaching events to things and got significantly lost and frustrated. Started By frumbert on Feb 5, 2007 at 11:35:17 PM |