Web Programming II Week 4
Web Programming II Week 4
By VERNYUY BASILE
TIMELINE/Week 4
1. Event handling methods
2. DOM Forms
1. Adding event 3. Using events to 4. Data/Form
listeners set CSS validation.
2. Removing event properties.
listeners
VERNYUY BASILE
1. Event handling methods.
VERNYUY BASILE
1. Adding event listeners.
VERNYUY BASILE
Examples
document.getElementById("myBtn").addEventListener("click",
myFunction);
function myFunction() {
alert ("Hello World!");
}
element.removeEventListener(eventType, eventHandler);
element: The HTML element from which you want to remove the
event listener.
eventType: The type of event you want to remove the listener for
(e.g., "click", "mouseover", "keydown").
eventHandler: The function that was used as the event listener.
VERNYUY BASILE
Example.
const myElement = document.getElementById("myElement");
function handleClick(event) {
alert("Clicked button!");
}
myElement.addEventListener("click", handleClick);
VERNYUY BASILE
3. Using events to set CSS properties.
VERNYUY BASILE
Example.
function handleMouseOver(event) {
myElement.style.backgroundColor = "yellow";
}
myElement.addEventListener("mouseover", handleMouseOver);
VERNYUY BASILE
2. DOM Forms.
VERNYUY BASILE
DOM Forms/Data validation.
DOM forms refer to the interaction between JavaScript and HTML forms
using the Document Object Model (DOM). This allows for dynamic form
validation, data manipulation, and user interaction enhancement.
VERNYUY BASILE
Key Elements of DOM Forms:
Form Element: The <form> tag defines a form element. It contains
input fields, buttons, and other elements that allow users to input data.
Input Elements: Various input elements are used to collect user input,
such as <input>, <textarea>, <select>, and <button>.
Event Listeners: JavaScript event listeners are attached to form
elements to capture user actions, like form submission, button clicks
etc.
VERNYUY BASILE
Example DOM Forms:
function formcontrol(){
let usernames = document.forms.myForm.names.value
let userpassword = document.forms.myForm.password.value
let usercpassoword = document.forms.myForm.cpassword.value
if(usernames==""){
document.getElementById("errornames").innerHTML="Please fill your names"
return false;
}
if(userpassword==""){
document.getElementById("errorpassword").innerHTML="Sorry you need a password"
return false;
}
if(usercpassoword==""){
document.getElementById("errorcpassword").innerHTML="Sorry confirm the password"
return false;
}
}
VERNYUY BASILE
Common DOM Form Interactions:
VERNYUY BASILE
Assignments
1. Create an HTML form with 4 input fields, change the
background color, text color with the click of a button.
Using js make sure the user do not submit the form with
empty fields.
End