firing HTML elements click events

How do I from javascript fire another element's (DIV, HREF, etc..) onclick event. Here is a sample that works in all other browsers but not in Safari (MAC and Windows releases). Is there something in the Safari DOM I am missing?

The HTML below isn't submitting correctly to this forum. The "click me to see something" is text inside of a SPAN. You can see the same issue if it's a DIV or Anchor, etc..

fireEvent and .click() aren't working for me.

Code Sample

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Safari Behavior : Click Event</title>

<script language="javascript" type="text/javascript">

function buttonClick()
{
//spanClick.fireEvent("onclick");
spanClick.click();
}

function clickEvent(x)
{
alert(x + " was clicked");
}

</script>

</head>
<body>
Click me to see something ....


<button id="oButton1" onclick="buttonClick();">Click to fire event</button>
</body>
</html>





Posted on Jun 25, 2007 10:52 AM

Reply
5 replies

Jun 28, 2007 6:14 AM in response to ra5ul

Hello,

Thanks for your reply. I realized the above code does not work in Firefox until I add the below snippet to make it Firefox compatible. Is there similar code I need to add for Safari to work appropriately?

if(navigator.userAgent.indexOf("Firefox") != -1)
{
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);
}
}


Windows XP Pro

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

firing HTML elements click events

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.