3

I need to simulate a click on a button using jquery.

Regular Javascript MouseEvents (mousedown and mouseup) do work, but I want jquery.

I have the div in a jquery object: (just a reference)

it was defined like

var $button = $(document.getElementById("iframe")...);

$button -> <div id="1" class="2" role = "button"><b>Action</b></div>

I've tried with $button.click() and $button.trigger('click') but both don't work.

I've read this, but provides no answer to this question.

Community
  • 1
  • 1
jacktrades
  • 7,224
  • 13
  • 56
  • 83

1 Answers1

6

Try this:

var e = document.createEvent("MouseEvents");
e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
$button[0].dispatchEvent(e);
Esailija
  • 138,174
  • 23
  • 272
  • 326
  • 3
    jQuery doesn't fire events attached with dom2 methods, sorry. It only fires its own events. And accidentally click/submit dom0 events in some cases as a result of calling the `.click()/.submit()` method on native dom elements where they exist. – Esailija Aug 01 '12 at 18:16
  • 1
    I think that's the sad answer – jacktrades Aug 01 '12 at 18:16