Unit 2-5 - Web Technology
Unit 2-5 - Web Technology
B.Tech – V Semester
UNIT II
M. Vijayakumar
Assistant Professor (SS)
School of Computing Sciences,
Department of Computer Science and Engineering
Handling Events
HTML allows event handler attributes, with JavaScript code, to be added to HTML
elements.
<!DOCTYPE html>
<html>
<body>
<button
onclick="document.getElementById('demo').innerHTML=Date()
">The time is?</button>
<p id="demo"></p>
</body>
</html>
In the next example, the code changes the content of its own element (using
this.innerHTML):
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<script>
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
<p id="demo"></p>
</body>
</html>
Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an
HTML element
onmouseout The user moves the mouse away from
an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the
page
Many different methods can be used to let JavaScript work with events:
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 12
onclick Event
Execute a JavaScript when a button is clicked:
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "YOU
CLICKED ME!";
}
</script>
</body>
</html>
</body>
</html>
<p>The function bigImg() is triggered when the user moves the mouse pointer over the image.</p>
<p>The function normalImg() is triggered when the mouse pointer is moved out of the image.</p>
<script>
function bigImg(x) {
x.style.height = "64px";
x.style.width = "64px";
}
function normalImg(x) {
x.style.height = "32px";
x.style.width = "32px";
}
</script>
</body>
</html>
<p>Press a key inside the text field to set a red background color.</p>
<script>
function myFunction() {
document.getElementById("demo").style.backgroundColor = "red";
}
</script>
</body>
</html>
Department of Computer science and Engineering CSB4301 - WEB TECHNOLOGY 19
<!DOCTYPE html>
<html>
<body>
<script>
function myFunction() {
alert("You pressed a key inside the input field");
}
</script>
</body>
</html>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Iframe is
loaded.";
}
</script>
</body>
</html>
<h1>Hello World!</h1>
<script>
function myFunction() {
alert("Page is loaded");
}
</script>
</body>
</html>