Javascript Events 1
Javascript Events 1
BLUR
<!DOCTYPE html>
<html>
<body>
<h1>HTML DOM Events</h1>
<h2>The form Event</h2>
<p id="demo">This example specifies how the blur event works on.</p>
<script>
function myFunction() {
alert("Input field lost focus.");
}
</script>
</body>
</html>
CLICK
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onclick Attribute</h2>
<p id="demo"></p>
</body>
</html>
FOCUS
<html>
<head> Javascript Events</head>
<body>
<h2> Enter something here</h2>
<input type="text" id="input1" onfocus="focusevent()" />
<script>
<!--
function focusevent()
{
document.getElementById("input1").style.background=" red";
}
//-->
</script>
</body>
</html>
ONCLICK
<html>
<head> JS Event Handler </head>
<body>
<script language="Javascript" type="text/Javascript">
function clickevent()
{
document.write("This is mouse event");
}
</script>
<form>
<input type="button" onclick="clickevent()" value="which event is it?"/> <br>
</form>
</body>
</html>
CHANGE
<html>
<body>
<label for="country">Select a country:</label>
<select id="country" onchange="handleChange()">
<option value="USA">USA</option>
<option value="Canada">Canada</option>
<option value="UK">UK</option>
<option value="India">India</option>
</select>
<p id="txt"></p>
<script>
function handleChange() {
// Perform actions when the dropdown selection changes
var selectedCountry = document.getElementById('country').value;
document.getElementById("txt").textContent=
"Selected country: "+selectedCountry;
}
</script>
</body>
</html>
SUBMIT
<html>
<body>
<form onsubmit="return validateForm()">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br/>
<input type="submit" value="Submit">
</form>
<script>
function validateForm() {
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
// Perform validation
if (username === "" || password === "") {
alert("Please fill in all fields");
return false; // Prevent form submission
}
alert("Form submitted! Username is:"+username+",Password is:"+password);
return true; // Allow form submission
}
</script>
</body>
</html>