Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What's the Mozilla equivalent to "myElement.click()" for images? 1

Status
Not open for further replies.

smsinger3

Programmer
Oct 5, 2000
192
US
Hi. I have this html in IE, but I can't figure out the W3C equivilent that works in Firefox.

Code:
<img id="QAddMLSArea" src="/images/QuickAdd.gif" onclick="LookupListQuickAdd('disMLSArea','MLSArea');">

<script language="javascript" type="javascript/text">
var ctrlAddButton = document.getElementById("QAddMLSArea");
ctrlAddButton.click();  //What can I use for Firefox???
</script>

Any suggestions?

Thanks!

-Steve
 
Add this in your page somewhere:
Code:
if(typeof HTMLElement != 'undefined' && !HTMLElement.prototype.click) {
   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);
   }
}

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

Finally, <. is a good thing!
 
[0] As an aside, there is a material typo on the type.

[1] The simplest would be to look at onclick itself as a function constructor. It is thereby cross-browser rightaway.
[tt]
<script language="javascript" type="[red]text/javascript[/red]">
var ctrlAddButton = document.getElementById("QAddMLSArea");
ctrlAddButton.[red]on[/red]click();
</script>
[/tt]
[2] As another aside, have to be careful on the timing for barely exposed script line of the sort. If in doubt, put some time delay. Here's another version with some adhoc time delay.
[tt]
<script language="javascript" type="text/javascript">
[blue]setTimeout("document.getElementById('QAddMLSArea').onclick()",500)[/blue]
</script>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top