James
James

Reputation: 305

How to trigger a button as clicked using jquery or jscript?

Is there a way to trigger a hidden button on an html markup page using jquery or jscript?

How would I do that if possible?

Thank you,

James

Upvotes: 4

Views: 3810

Answers (4)

Steerpike
Steerpike

Reputation: 17544

Try this (requires jquery):

$("button").trigger('click');

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827218

With jQuery:

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

or:

$('#buttonId').trigger('click');

With plain JavaScript:

document.getElementById('buttonId').onclick();

Since you're using ASP .NET you might want to get the button id by using the ClientID server-side property of the control:

$('#<%=Button.ClientID %>').click(); 

Or:

document.getElementById('<%=Button.ClientID %>').onclick(); 

Upvotes: 5

Matthew Vines
Matthew Vines

Reputation: 27561

If I understand correctly, you want to fire an event programmatically. This is usually not necessary. You can very easily move the button click even code to a new method, and have the event invoke that method. You can then also call the method in the code that would want to fire the button click even programmatically.

Upvotes: 0

Ken Browning
Ken Browning

Reputation: 29091

$('#example-button-id').click();

Upvotes: 2

Related Questions