Saturday, 5 January 2013

How to use javascript events in HTML

Javascript has mainly two events that can be used in HTML. They are as follows:

1. onclick Event: The onclick event occurs when the user clicks on an element. The example is as follows:

<!DOCTYPE html>
<html>
<head>
<script>
function changetext(id){
id.innerHTML="Ooooopsss!";
}
</script>
</head>
<body>
<h1 onclick="changetext(this)">Click on this text!</h1>
</body>
</html>

2. onMouseout Event: The onmouseout event occurs when a user moves the mouse pointer out of an element. The example is as follows:

<html>
<head>
<script type="text/javascript">
<!--
function popup(){
alert("Hello World!")}
//-->
</script>
</head>
<body>
<input type="button" value="Click Me!" onclick="popup()"><br/>
<a href="#" onmouseover="" onMouseout="popup()">Hover Me!</a>
</body>
</html>

No comments:

Post a Comment