The addEventListener() method attaches an event handler to the specified element.
The addEventListener() method attaches an event handler to an element without overwriting existing event handlers.
You can add many event handlers to one element.
You can add many event handlers of the same type to one element, i.e two "click" events.
You can add event listeners to any DOM object not only HTML elements. i.e the window object.
The addEventListener() method makes it easier to control how the event reacts to bubbling.
When using the addEventListener() method, the JavaScript is separated from the HTML markup, for better readability and allows you to add event listeners even when you do not control the HTML markup.
You can easily remove an event listener by using the removeEventListener() method.
Syntax: element.addEventListener(event, function, useCapture);
CODING:The addEventListener() method attaches an event handler to an element without overwriting existing event handlers.
You can add many event handlers to one element.
You can add many event handlers of the same type to one element, i.e two "click" events.
You can add event listeners to any DOM object not only HTML elements. i.e the window object.
The addEventListener() method makes it easier to control how the event reacts to bubbling.
When using the addEventListener() method, the JavaScript is separated from the HTML markup, for better readability and allows you to add event listeners even when you do not control the HTML markup.
You can easily remove an event listener by using the removeEventListener() method.
Syntax: element.addEventListener(event, function, useCapture);
<!DOCTYPE html>
<html>
<body>
<p id="p"></p>
<button id="myBtn1">1st Type</button>
<script>
window.addEventListener("resize", function(){
document.getElementById("p").innerHTML = Math.random();});
document.getElementById("myBtn1").addEventListener("click", function(){alert("1st Type");});
</script>
<button id="myBtn2">2nd Type</button>
<script>
document.getElementById("myBtn2").addEventListener("click", Fun);
function Fun() {
alert ("2nd Type");
}
</script>
<button id="myBtn3">3rd Type</button>
<script>
var x = document.getElementById("myBtn3");
x.addEventListener("click", Fun1);
x.addEventListener("click", Fun2);
x.addEventListener("mouseover", Fun3);
function Fun1() {alert("Fun1 Call");}
function Fun2() {alert("Fun2 Call");}
function Fun3() {alert("You do not Click,LOL");}
</script>
</body>
</html>
<html>
<body>
<p id="p"></p>
<button id="myBtn1">1st Type</button>
<script>
window.addEventListener("resize", function(){
document.getElementById("p").innerHTML = Math.random();});
document.getElementById("myBtn1").addEventListener("click", function(){alert("1st Type");});
</script>
<button id="myBtn2">2nd Type</button>
<script>
document.getElementById("myBtn2").addEventListener("click", Fun);
function Fun() {
alert ("2nd Type");
}
</script>
<button id="myBtn3">3rd Type</button>
<script>
var x = document.getElementById("myBtn3");
x.addEventListener("click", Fun1);
x.addEventListener("click", Fun2);
x.addEventListener("mouseover", Fun3);
function Fun1() {alert("Fun1 Call");}
function Fun2() {alert("Fun2 Call");}
function Fun3() {alert("You do not Click,LOL");}
</script>
</body>
</html>
Comments
Post a Comment