<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
Getting the class of the element that
fired an event using JavaScript.
</
title
>
<
style
>
#div {
background: green;
height: 100px;
width: 200px;
margin: 0 auto;
color: white;
}
#gfg {
color: green;
font-size: 20px;
font-weight: bold;
}
</
style
>
</
head
>
<
body
style
=
"text-align: center;"
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
p
>
Click on the DIV box or button to get Class of
the element that fired click event.
</
p
>
<
div
class
=
"DIV"
id
=
"div"
onClick
=
"GFG_click(this.getAttribute('class'));"
>
This is Div box.
</
div
>
<
br
/>
<
button
class
=
"button"
id
=
"button"
onClick
=
"GFG_click(this.getAttribute('class'));"
>
Button
</
button
>
<
p
id
=
"gfg"
></
p
>
<
script
>
var el_down = document.getElementById("gfg");
function GFG_click(className) {
// This function is called, when the
// click event occurs on any element.
// get the classname of element.
el_down.innerHTML = "Class = " + className;
}
</
script
>
</
body
>
</
html
>