

This is the preferred technique for handling the event in a derived class. The OnClick method also allows derived classes to handle the event without attaching a delegate. For more information, see Handling and Raising Events.

In JavaScript, you invoke a function by calling its name, then you put a parenthesis after the function identifier (the name). Raising an event invokes the event handler through a delegate. The value it takes, which is the function you want to execute, says it all, as it is invoked right within the opening tag. Note that the onclick attribute is purely JavaScript. You place the JavaScript function you want to execute inside the opening tag of the button. This could be when a user submits a form, when you change certain content on the web page, and other things like that. The onclick event executes a certain functionality when a button is clicked.
Scriptcase open form with onclick event how to#
How to Use the onclick event in JavaScript Then we'll see how the more modern "click" eventListner works, which lets you separate the HTML from the JavaScript. In this tutorial, we are going to explore the two different ways of executing click events in JavaScript using two different methods.įirst, we'll look at the traditional onclick style that you do right from the HTML page. Buttons, on the other hand, are usually manipulated by JavaScript events so they can trigger certain functionality. the form's submit event instead of the button's click event). Links take you to a certain part of the page, another page of the website, or another website entirely. That said, I'd try to avoid the intrinsic event attributes in favour of unobtrusive JS with a library (such as YUI or jQuery) that has a good event handling API and tie into the event that really matters (i.e. Whenever you visit a website, you'll probably click on something like a link or button.
