Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 select text on specific input

Tags:

angular

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?

like image 572
Jon Saw Avatar asked Feb 17 '17 07:02

Jon Saw


People also ask

How do you highlight text in input?

For selecting the content of an input (with highlight), use: el. select() .

How to bind input value in Angular?

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.

What is keyup event in Angular?

(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.

How to give input in Angular?

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!


5 Answers

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.

like image 85
Fredrik Lundin Avatar answered Nov 08 '22 19:11

Fredrik Lundin


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();
}
like image 23
Sangmin Lee Avatar answered Nov 08 '22 21:11

Sangmin Lee


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();
  }
}
like image 6
Vasyl Kobetiak Avatar answered Nov 08 '22 19:11

Vasyl Kobetiak


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

  1. Add a local variable name
<input type="text" #myInput (focus)="$event.target.select()" />
  1. Use 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.

like image 10
pcnate Avatar answered Nov 08 '22 19:11

pcnate


If you are using mat-input, you can try onfocus="this.select()"

<input matInput onfocus="this.select()">
like image 1
NobodySomewhere Avatar answered Nov 08 '22 19:11

NobodySomewhere