Programmatically click a button using JavaScript (on an website)

Posted on 2011-10-16
Last Modified: 2012-05-12
I have an (VB) website.  I need to create a JavaScript function that would mimic the act of clicking an image button -- i.e. activate the subroutine that is triggered by clicking the button.

Here is the code for the button.  As you can see, clicking the button activates the "GoCheckout" subroutine.  That is what I need a JavaScript function to do.

<asp:ImageButton ID="btnCheckOut" runat="server"  AlternateText="Checkout" onClick="GoCheckout"/>

Open in new window

Here is what the code for the button looks like at runtime:
 <input type="image" name="ctl00$ContentPlaceHolder1$btnCheckOut" id="ctl00_ContentPlaceHolder1_btnCheckOut" src="/images/checkout.png" alt="Checkout"  />

Open in new window

Any assistance would be appreciated.

Question by:Jorhal
    LVL 81

    Accepted Solution

    Use :


    or :

    document.getElementById("<%= btnCheckOut.ClientID %>").click();
    LVL 9

    Expert Comment

    The javascript function below "GoCheckout()" will trigger an alert box when btnCheckOut
    is clicked!

               <script language="javascript">
                      function GoCheckout(){
                            // this example is triggers alert box when button is clicked
                            // replace with whatever Javascript code you need to run
                            alert("The Checkout Button was clicked");
    LVL 17

    Expert Comment

    Very simple In page_load subroutine Add

    btnCheckOut.Attributes.Add("onclick", "GoCheckout();")

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now