Published on: 27 June, 2014

Javascript Events

Posted by andy | 1207 views 0 likes 0 favourites 0 comments
Tumblr Digg Google Plus

Any interaction in the html called by Javascript are events. Those events can be triggered by buttons or links or any html tags. Events interaction can be click,mouseover, mouseout, mouseenter, mouseleave, mousemove, mouseup, blur, focus etc.

Example of Event Handler

Please see the following example, where we add an event click in a button.

    <input type="button" value="Click Me" onclick="alert('You just click me')"/>

Click the following button to see the quick demo.

There is an alternative way to add an event handler via javascript. You can use the following javascript code example.

// Note the btn is the id of the input button
var myButton = document.getElementById("btn");
myButton.onclick = function(){ 
   alert("You just click me");
};

//alternative you can use the built in addEventListener as well

myButton.addEventListerner("click", function(event){
   alert("You just click me");
});

How to remove the event handler?

If you are creating the dynamic event handler via javascript, you can just set the onclick value to null. See below example:

// Note the btn is the id of the input button
var myButton = document.getElementById("btn");
myButton.onclick = null;
Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Related Articles
Published on: 20 June, 2016
Google Analytics is one of the best free analytics site tracker. It gives you an idea the statistic of your visitors like the country they come from, the URL or keyword they search to go to your site or even the demographic or ages of your visitors. If you are a developer, you definitely do not want to track any data when you are in development phase. In this tutorial, you will learn how to disable google analytics for local development by using a javascript code if condition.
Published on: 13 May, 2016
To convert a number value into money format in Javascript can be done easily using Javascript. Firstly you will need to use the built in toFixed function that will return a number value into two decimal point and then using a regular expression to reformat the value.