Blankman
Blankman

Reputation: 266988

How can I manually fire an event that I wired up using jQuery?

How do I manually fire a click event on a button that I previously wired up using jQuery?

Upvotes: 0

Views: 1188

Answers (3)

ahmedelhamdi
ahmedelhamdi

Reputation: 11

Youe can use an ID or class name as a selector to select the element

Then you can do it as below

$(".className").click();

$("#ID").click();

it works with me and you can try it in chrome console

Upvotes: 0

Poldon
Poldon

Reputation: 365

One way I've done it is:

$('#pSearch').click();

The pSearch is the ID of the button I want to click.

Upvotes: 1

cgp
cgp

Reputation: 41381

Use Trigger. (link to documentation for trigger)

$("button:first").click(function () {
  update($("span:first"));
});
$("button:last").click(function () {
  $("button:first").trigger('click');

  update($("span:last"));
});

function update(j) {
  var n = parseInt(j.text(), 10);
  j.text(n + 1);
}

Upvotes: 6

Related Questions