Lecture 17 Java Script Part4
Lecture 17 Java Script Part4
Module 2
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick = displayDate;
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
• The content of the <h1> element is
<h2>JavaScript HTML Events</h2>
<h2 onclick="changeText(this)">Click on this text!</h2> changed when a user clicks on it.
<script>
function changeText(id) { • Here function is called from the
id.innerHTML = "Ooops!";
} event handler
</script>
</body>
</html>
</body>
</html>
<script>
function upperCase() {
The upperCase() function will be called
const x = document.getElementById("fname");
x.value = x.value.toUpperCase(); when a user changes the content of an
}
</script> input field.
</body>
</html>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
<script>
function mOver(obj) {
obj.innerHTML = "Thank You"
}
function mOut(obj) {
obj.innerHTML = "Mouse Over Me"
}
</script>
</body>
</html>
</body>
</html>
</body>
</html>
• We can add
• many event handlers to one element.
• many event handlers of the same type to one element, i.e two "click" events.
• 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.
Syntax:
• The first parameter is the type of the event (like "click" or "mousedown" or any other HTML DOM Event.)
• The second parameter is the function we want to call when the event occurs.
• The third parameter is a boolean value specifying whether to use event bubbling or event capturing. (optional).
Note that you don't use the "on" prefix for the event; use "click" instead of "onclick".
<h2>JavaScript addEventListener()</h2>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript addEventListener()</h2>
<p>This example uses the addEventListener() method to attach a click event to a button.</p>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", displayDate);
function displayDate() {
document.getElementById("demo").innerHTML = Date();
}
</script>
</body>
</html>
<h2>JavaScript addEventListener()</h2>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
x.addEventListener("click", someOtherFunction);
function myFunction() {
alert ("Hello World!");
}
function someOtherFunction() {
alert ("This function was also executed!");
}
</script>
</body>
</html>
Prepared By: Mr. Vaibhav
<!DOCTYPE html>
<html>
Many Event Handler to
<body> same element
<h2>JavaScript addEventListener()</h2>
<p>This example uses the addEventListener() method to add many events on • You can add events of different types to
the same button.</p>
the same element
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("mouseover", myFunction);
x.addEventListener("click", mySecondFunction);
x.addEventListener("mouseout", myThirdFunction);
function myFunction() {
document.getElementById("demo").innerHTML += "Moused over!<br>";
}
function mySecondFunction() {
document.getElementById("demo").innerHTML += "Clicked!<br>";
}
function myThirdFunction() {
document.getElementById("demo").innerHTML += "Moused out!<br>";
}
</script>
</body>
</html> Prepared By: Mr. Vaibhav
Passing Parameters • When passing parameter values, use an
<!DOCTYPE html>
<html> "anonymous function" that calls the
<body>
specified function with the parameters
<h2>JavaScript addEventListener()</h2>
<p id="demo"></p>
<script>
let p1 = 5;
let p2 = 7;
document.getElementById("myBtn").addEventListener("click",
function() {
myFunction(p1, p2);
});
function myFunction(a, b) {
document.getElementById("demo").innerHTML = a * b;
}
</script>
• Bubbling: In bubbling the inner most element's event is handled first and then the outer
• Capturing: In capturing the outer most element's event is handled first and then the inner
Event propagation is a way of defining the element order when an event occurs.
• Scenario: If you have a <p> element inside a <div> element, and the user clicks on the <p> element,
which element's "click" event should be handled first?
• In bubbling: the <p> element's click event is handled first, then the <div> element's click event. (Bottom to top)
• In capturing: the <div> element's click event will be handled first, then the <p> element's click event. (Top to
Bottom)
Prepared By: Mr. Vaibhav
Event Bubbling or Event Capturing?
Syntax:
With the addEventListener() method we can specify the propagation type by using the "useCapture"
parameter:
• The default value is false, which will use the bubbling propagation,
• when the value is set to true, the event uses the capturing propagation.
document.getElementById("myDiv1").addEventListener("cl
ick", function() {
alert("You clicked the orange element!");
}, false);
document.getElementById("myP2").addEventListener("clic
k", function() {
alert("You clicked the white element!");
}, true);
document.getElementById("myDiv2").addEventListener("cl
ick", function() {
alert("You clicked the orange element!");
}, true);
</script>
</body>
</html>
<div id="myDIV">
<p>This div element has an onmousemove event handler that displays a random number
every time you move your mouse inside this orange field.</p>
<p>Click the button to remove the div's event handler.</p>
<button onclick="removeHandler()" id="myBtn">Remove</button>
</div>
<p id="demo"></p>
<script>
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
function myFunction() {
document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler() {
Prepared
document.getElementById("myDIV").removeEventListener("mousemove", myFunction); By: Mr. Vaibhav