Tutorials » Javascript tutorials »

Calling JavaScript function in an Event

This article explains about calling a simple Java script function in an event .

Let's write a simple Javascript function and make it to fire on click event of command button.
Event is something that can respond to particular action.

Each HTML control has got certain kind of events which can fires JavaScript functions. That JavaScript function needs to be assigned to event. We place event in html tag of the control itself. For example onclick event fires when we click on that control by any cast.

Some of the commonly used events are:

  • onclick
  • onblur
  • onchange
  • onkeypress
  • onmouseover
  • onselect
  • onsubmit
  • onfocus


This event is applicable to controls like button, radio button, check box, submit button etc.




function f()


alert('Hello! How are you?');





<input type ="button" value ="Click" onclick ="return f();" >


Execution of the Page:

When alert.html is executed form is loaded with a command button. Until and unless JavaScript function is called it doesn’t get executed. Here we have assigned f () for onclick. So JavaScript function is called as soon as we click on the button and the code in the function is executed. Hence we click on button an alert box is executed.

Next Chapter: A simple login Validation with JavaScript
Previous Chapter: prompt, confirm ,alert all together
More Chapters: Javascript Tutorials
More Tutorials: Tutorial Index

Top Contributors
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India