With jQuery, we would select all contents of a specific input on-submit this way:
<form id="target">
<input id="input-1" type="text" value="some value" />
<form>
$("#target").submit((event) => {
event.preventDefault();
$("#input-1").select();
});
How do we accomplish this with Angular 2?
For selecting the content of an input (with highlight), use: el. select() .
You can use Angular event bindings to respond to any DOM event. Many DOM events are triggered by user input. Binding to these events provides a way to get input from the user. To bind to a DOM event, surround the DOM event name in parentheses and assign a quoted template statement to it.
(keyup) is an Angular event binding to respond to any DOM event. It is a synchronous event that is triggered as the user is interacting with the text-based input controls. When a user presses and releases a key, the (keyup) event occurs.
The first step to passing data into an Angular component is to create a custom property to bind to. This is done via “input” binding to pass data from one component to another (typically parent to child). This custom input binding is created via the @Input() decorator!
You can easily do it in the template like this:
<input type="text" (click)="$event.target.select()" />
or add a local variable to your element and reference that instead:
<input type="text" #myInput (click)="myInput.select()" />
The benefit of the second approach is that by setting a local variable name to your element, other elements can reference that variable as well.
I figured out a way without using an event listener...
<form id="target">
<input id="input-1" #input-1 type="text" value="some value" />
<form>
component.ts
@ViewChild('input-1') inputOne: ElementRef;
selectInput() {
const inputElem = <HTMLInputElement>this.inputOne.nativeElement;
inputElem.select();
}
You can create directive like below and use it where you need:
import {Directive, ElementRef, HostListener} from '@angular/core';
@Directive({
selector: '[appSelectOnFocus]'
})
export class SelectOnFocusDirective {
constructor(private elementRef: ElementRef) {
}
@HostListener('focus', null) onFocus() {
this.elementRef.nativeElement.select();
}
}
A better idea for focus events is to use the focus event rather than the click event.
<input type="text" (focus)="$event.target.select()" />
To focus from inside your component logic you will need to add a few more things
<input type="text" #myInput (focus)="$event.target.select()" />
ViewChild
to access your DOM element@ViewChild("#myInput") myInputField: ElementRef;
editMyInputField(): void {
this.myInputField.nativeElement.focus();
}
To trigger the selection programmatically from outside the component you will need to use @Input
. Something along the lines of:
@Input() set myInputFocus() {
this.editMyInputField();
}
All this should still be working with Angular 8. I did not test any of this on Angular 2 or 4.
It would get a bit more complicated if you're using dynamic inputs. For such cases, I have used a form as the parent reference rather than declaring many local variables in the component.
If you are using mat-input, you can try onfocus="this.select()"
<input matInput onfocus="this.select()">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With