The onclick event occurs when the user clicks on an element.
Syntax In HTML:
elements onclick="myScript"
In JavaScript:
object.onclick = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("click", myScript);
Different types of way how to define onclick Event in program.
Syntax In HTML:
elements onclick="myScript"
In JavaScript:
object.onclick = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("click", myScript);
Different types of way how to define onclick Event in program.
<!DOCTYPE html>
<html>
<style>
.c{
color:white;
font-size:20px;
background-color:lightgreen
}
</style>
<body>
<button id="c">Click Me And Changed ME!</button>
<p id="p" onclick="shadow(this)" >Click anywhere in this window to change the background color of body.</p>
<p onclick="getElementById('p').style.fontSize = '45pt'">here,click...</p>
<script>
document.getElementById("c").onclick= function() {changeButton()};
function changeButton(){document.getElementById("c").classList.toggle("c");}
window.onclick = changeColor;
function changeColor(){document.getElementsByTagName("BODY")[0].style.backgroundColor ="lightgreen";}
function shadow(element) {element.style.color="green";}
</script>
</body>
</html>
<html>
<style>
.c{
color:white;
font-size:20px;
background-color:lightgreen
}
</style>
<body>
<button id="c">Click Me And Changed ME!</button>
<p id="p" onclick="shadow(this)" >Click anywhere in this window to change the background color of body.</p>
<p onclick="getElementById('p').style.fontSize = '45pt'">here,click...</p>
<script>
document.getElementById("c").onclick= function() {changeButton()};
function changeButton(){document.getElementById("c").classList.toggle("c");}
window.onclick = changeColor;
function changeColor(){document.getElementsByTagName("BODY")[0].style.backgroundColor ="lightgreen";}
function shadow(element) {element.style.color="green";}
</script>
</body>
</html>
Comments
Post a Comment