JavaScript onclick Event

The onclick event in JavaScript simply triggers when you click a specific control, such as button control.

JavaScript onclick Event Example

Here is an example demonstrates onclick event in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
   <title>JavaScript onclick Event</title>
</head>
<body>

<h3>JavaScript onclick Event Example</h3>
<form name="myform">
   Name: <input type="text" id="name" value=" "><br/>
   <button onclick="alert('Hi ' + document.getElementById('name').value + ', Welcome to fresherearth.com!')">Click Here</button>
</form>

</body>
</html>

Here is the sample output of the above JavaScript onclick event example. This is the initial output:

javascript onclick event

Now enter your name, and press on Click Here button, after pressing on that button, you will see an alert box showing an alert message as shown in the following snapshot:

javascript onclick event example

Here is the live demo output produced by the above onclick event in JavaScript.

Name:

JavaScript Online Test


« Previous Tutorial Next Tutorial »