Angular Material Input Select All on Click?
Asked Answered
B

3

7

Pretty straight-forward question. When I click on an input field I want to select-all the text so when I start typing it over-writes what was previously there. Anyone know how to do this?

Beatty answered 13/1, 2018 at 20:22 Comment(0)
T
32

Seems the following should work:

<input matInput (click)="$event.target.select()">

Example

Tampere answered 13/1, 2018 at 20:38 Comment(2)
Indeed it does! tyBeatty
I realize the question asked specifically for when someone "clicks" on it... but I think it would be better to do with on focus: (focus)="$event.target.select()" That way if you are clicking around inside the field to move a cursor you wont end up re-selecting everything.Completion
G
8

What worked best for me is onfocus="this.select()"

<input matInput onfocus="this.select()">
Gilligan answered 1/2, 2021 at 12:28 Comment(0)
J
1

If you are using angular material 1.x, you can use md-select-on-focus

<md-input-container>
  <label>Auto Select</label>
  <input type="text" md-select-on-focus>
</md-input-container>

This link can help you md-select-on-focus

Juniper answered 13/1, 2018 at 20:31 Comment(1)
FYI: This is AngularJS, Angular2 does not have thisPomegranate

© 2022 - 2024 — McMap. All rights reserved.