DOM Events in Angular
Learn how Angular works with browser DOM events such as click, input, change, keyboard and mouse events.
CIIT Training Institute
DOM events allow Angular applications to respond to actions performed by users on HTML elements.
What are DOM Events?
DOM events are actions or occurrences that happen on HTML elements because of user interaction or browser activity.
For example, when Sam clicks a button, Pradnya types in an input box, or Samiksha presses a keyboard key, the browser generates an event.
Common DOM Event Categories
Mouse Events
Events generated through mouse interactions.
click, dblclick, mouseenter
Keyboard Events
Events generated when keyboard keys are pressed or released.
keydown, keyup
Input Events
Events generated when values inside input controls change.
input, change
Form Events
Events generated while interacting with forms.
submit, reset
1. Click Event
The click event occurs when the user clicks an HTML element such as a button.
<button (click)="showMessage()">
Click Me
</button>
showMessage() {
console.log(
'Welcome to CIIT Training Institute'
);
}
CIIT Training Institute
Clicking the button executes the showMessage() method.
2. Double Click Event
The dblclick event occurs when an element is double-clicked.
<button (dblclick)="showDetails()">
Double Click
</button>
showDetails() {
console.log(
'Student details opened'
);
}
3. Input Event
The input event occurs whenever the value of an input element changes while the user is typing.
<input
type="text"
(input)="onInput($event)"
placeholder="Enter student name">
onInput(event: Event) {
const value =
(event.target as HTMLInputElement).value;
console.log(value);
}
Angular can capture every input event.
4. Change Event
The change event occurs when the value of a form control changes.
<select (change)="selectCourse($event)">
<option>Angular Full Stack</option>
<option>.NET Full Stack</option>
<option>Java Full Stack</option>
</select>
selectCourse(event: Event) {
const course =
(event.target as HTMLSelectElement).value;
console.log(course);
}
5. Keyboard Events
Angular can handle keyboard events using keydown and keyup.
Key Down
Occurs when a keyboard key is pressed.
(keydown)
Key Up
Occurs when a pressed keyboard key is released.
(keyup)
<input
(keydown)="keyPressed($event)"
placeholder="Press a key">
keyPressed(event: KeyboardEvent) {
console.log(
'Key:',
event.key
);
}
6. Mouse Events
Angular can respond to different mouse interactions.
| Event | Syntax | Description |
|---|---|---|
| Click | (click) |
User clicks an element |
| Double Click | (dblclick) |
User double-clicks an element |
| Mouse Enter | (mouseenter) |
Mouse enters an element |
| Mouse Leave | (mouseleave) |
Mouse leaves an element |
| Mouse Down | (mousedown) |
Mouse button is pressed |
| Mouse Up | (mouseup) |
Mouse button is released |
7. Form Events
Angular can handle form-related events such as submit.
<form (submit)="submitForm()">
<input
type="text"
placeholder="Student Name">
<button type="submit">
Submit
</button>
</form>
submitForm() {
console.log(
'Student form submitted'
);
}
Working with the Event Object
Angular provides the $event object when the event details are required.
Complete Example – CIIT Student Events
selectedStudent = '';
selectStudent(name: string) {
this.selectedStudent = name;
}
showWelcome() {
console.log(
'Welcome to CIIT Training Institute'
);
}
<h2>
CIIT Training Institute
</h2>
<p>
Select a student:
</p>
<button
(click)="selectStudent('Sam')"
class="btn btn-primary">
Sam
</button>
<button
(click)="selectStudent('Pradnya')"
class="btn btn-primary">
Pradnya
</button>
<button
(click)="selectStudent('Samiksha')"
class="btn btn-primary">
Samiksha
</button>
<p>
Selected:
{{ selectedStudent }}
</p>
CIIT Training Institute
Select a student:
DOM Event Handling Best Practices
- Use the correct DOM event for the required user interaction.
- Keep event handler methods simple and readable.
- Use $event only when event information is required.
- Use meaningful method names such as selectStudent().
- Avoid putting complex business logic directly inside templates.
- Keep reusable application logic inside appropriate services.
- Test mouse, keyboard, input and form events properly.
Summary
DOM events allow Angular applications to respond to user interactions with HTML elements.
Important DOM events include click, dblclick, input, change, keydown, keyup, mouseenter, mouseleave and submit.
Angular event binding connects these browser events with component methods and application logic.