Events (CSS) 3rd Year
Events (CSS) 3rd Year
Events are actions recognized by the browser, usually triggered by user interactions or browser
activities.
keypress: Triggered when a key is pressed and held (works for character keys).
When it Occurs: Fires while the key is pressed down (deprecated for non-character keys)
Properties Available: Similar to keydown, but primarily used for capturing character input.
document.addEventListener("keydown", function() {
});
}); </html>
Button Events in JavaScript
Definition: Button events are triggered when users interact with buttons on the webpage.
mouseover/mouseout: Fires when the mouse enters or leaves the button area.
</html>
Practical Uses of Event Handling
Interactive Elements: Enabling actions like dropdown menus, sliders, and tabs.
<script type="text/javascript">
// Function to handle the selection change in the dropdown <!-- Dropdown list with an 'onchange' event handler -->
<option value="OnePlus">OnePlus</option>
// Display an alert showing the selected item
<option value="Google Pixel">Google Pixel</option>
alert("You have selected " + selected);
<option value="Xiaomi">Xiaomi</option>
}
</select>
</script>
</body>
</head>
Practical Use Cases:
Form Handling: Change the form's appearance or submit different data based on
the user's selections.
Dynamic Content Loading: Load specific content or settings when a user selects
an option.
Dynamically changing option lists help create Dynamically changing lists can be used to give
interactive and responsive forms. When the real-time feedback or adapt the options
options in a dropdown list change based on user available based on other form fields. For
input (such as selecting a radio button or instance, in a booking form, the list of available
checkbox), it makes the interface feel more time slots can change based on the selected
tailored and user-friendly. date, giving users a more accurate and current
view
if (elementValue == 1) {
<html>
// Add options for fruits
<head>
optionList.innerHTML = ""; }
</script>
<!-- Radio buttons to choose between Fruits and
<body>
Vegetables -->
<form name="myform" action="" <input type="radio" name="group" value="1"
method="post"> checked="true" onclick="updateList(this.value)">Fruits
<option value="1">Mango</option>
<input name="Reset" value="Reset" type="reset">
<option value="2">Banana</option>
</p>
</select>
</form>
<br>
</body>
</html>
Evaluating checkbox selections
1. Collect User Preferences: Allows users to choose multiple options, like favorite items or
preferences.
3. Conditional Actions: Enables performing specific actions based on the user's selections.
4. Form Validation: Helps ensure required selections are made (e.g., "Choose at least two
options").
<html>
<script type="text/javascript"> }
with (document.forms.myform) {
<!-- Checkbox inputs for different fruits -->
// Check if each checkbox is selected, then add its value to
<input type="checkbox" name="a" value="Apple">Apple
'x'
<input type="checkbox" name="b" value="Banana">Banana
if (a.checked == true) x += a.value + " ";
}
<!-- Button to trigger the selection function -->
</form>
</body>
</html>
Changing labels dynamically
Dynamic Label Changes: It allows updating labels or button text on the fly based
on user actions. This makes the user interface more interactive and responsive.
Improves User Experience: Users can see relevant options or instructions based
on their selections without needing to reload the page.
if (elementValue === "Cars") {
optionList[0].value = "Sedan";
<title>HTML Form</title>
optionList[1].text = "SUV";
<script type="text/javascript">
optionList[1].value = "SUV";
// Function to update the list based on
selected value } else if (elementValue === "Bikes") {
}
</head>
<body>
<p>
<option value="Sedan">Sedan</option>
<option value="SUV">SUV</option>
</select>
<br>
</p>
</form>
</body>
</html>
Manipulating form elements
Dynamic Interaction: Allows users to interact with forms in real-time, providing instant feedback based on their
input.
Validation: Ensures that user inputs meet specific criteria before submission, enhancing data integrity.
Automatic Field Updates: Enables automatic generation of values in one field based on inputs from another
field
(e.g., generating an email address from the first and last names).
User Experience: Improves user engagement by making forms more intuitive and user-friendly.
Form Customization: Allows developers to tailor forms based on user choices, making them more relevant to
the task at hand.
<html> </head>
<head> <body>
<title>HTML Form</title> <form name="myform" action="" method="post">
<script type="text/javascript"> <p>
function addLocation() {
City: <input type="text" name="City"/><br>
with (document.forms.myform) {
Country: <input type="text"
if (City.value.length > 0 && Country.value.length name="Country"/><br>
> 0) {
Location: <input type="hidden"
// Concatenate city and country to create a name="Location"/><br>
location string
<input name="Submit" value="Submit"
Location.value = City.value + ", " + type="button" onclick="addLocation()"/>
Country.value;
</p>
}
</form>
}
</body>
}
</html>
Intrinsic JavaScript Functions
Definition: Intrinsic JavaScript functions are built-in methods provided by the
JavaScript language that allow developers to manipulate web elements and
manage events without the need for additional libraries or frameworks.
Usage in Forms: They can be used to handle form submissions, reset forms, and
validate user input seamlessly.
Examples:
<head>
<title>HTML Form</title>
</head>
<body>
<p>
<img src="submit.jpg"
onclick="javascript:document.forms.myform.submit()" alt="Submit"/>
<img src="reset.jpg"
onclick="javascript:document.forms.myform.reset()" alt="Reset"/>
</p>
</form>
</body>
Disabling Elements
Definition: Disabling elements in HTML refers to preventing user interaction with form elements (like
input fields, buttons, etc.) by setting their state to "disabled." This is often used to indicate that certain
actions cannot be performed until specific conditions are met.
Usage:
Form Control: Disabled form elements do not submit their values with the form, making them useful for
controlling user input.
User Experience: Disabling inputs can help guide users by preventing them from interacting with
elements that should not be available at a particular time.
<html>
<head>
<script type="text/javascript">
function ToggleDisabled() {
if ('disabled' in text) {
</script>
</head>
<body>
</body>
Read-Only Elements
Definition: Read-only elements in HTML allow users to view content without the ability to modify
it. This is useful for displaying information that should not be changed by the user while still
allowing them to interact with the element (e.g., copying the content).
Usage:
Text Areas: Read-only text areas are commonly used to display pre-filled or calculated data that
the user should not alter.
User Interface: Using read-only elements enhances the user experience by clearly indicating
that certain data is informational and cannot be changed.
<html>
<head>
<script type="text/javascript">
function changeState() {
</script>
</head>
<body>
</body>
</html>