James123
James123

Reputation: 11652

Call submit button function from jquery?

I want call OnClick function on page load with out user clicking. Can we do that in jQuery or javascript?

     <input name="ctl00$PlaceHolderMain$ButtonSection$RptControls$BtnSubmit" 
        class="ms-ButtonHeightWidth" 
        id="ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit" 
        accessKey="o" onclick="javascript:WebForm_DoPostBackWithOptions(new 
        WebForm_PostBackOptions("ctl00$PlaceHolderMain
                                $ButtonSection$RptControls$BtnSubmit", 
                                "", true, "", "", false, true))" 
        type="button" value="OK"/>

Upvotes: 1

Views: 16411

Answers (6)

MikeM
MikeM

Reputation: 27405

var $btnSubmit = $('#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit');

// Either method will work:

// Method 1: jQuery has a click event pre-defined
$btnSubmit.click();

// Method 2: some events (such as newly defined HTML5 events) 
// may not be pre-defined, so .trigger('[event]') is a way to 
// explicitly invoke that event
$btnSubmit.trigger('click');

Upvotes: 0

JohnFx
JohnFx

Reputation: 34909

You imply in the question title that the button you want to click is a submit button. If so you would be better off calling the submit method of the form instead of the click event of the submit button.

document.forms["myform"].submit()

Upvotes: 1

Hector Sanchez
Hector Sanchez

Reputation: 2317

Or with javascript inside the body:

<body onload="javascript:document.getElementById('ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit').click()" ></body>

The trick is to get the element and call the click method.

Greetings.

Upvotes: 0

Amin Eshaq
Amin Eshaq

Reputation: 4024

You can try doing a $("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").trigger('click'); This would emulate a click on the button more info here

Upvotes: 1

James Allardice
James Allardice

Reputation: 165951

$(document).ready(function() {
    $("#ctl00_PlaceHolderMain_ButtonSection_RptControls_BtnSubmit").click();
});

This will trigger the click event on the element with the supplied id, and it will run when the document is fully loaded.

Upvotes: 5

Craig M
Craig M

Reputation: 5628

You can call trigger and pass the type of event to trigger.

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

Upvotes: 1

Related Questions