I am using simple form-field input
component as in below code
<mat-form-field class="example-form-field" >
<input matInput type="search" placeholder="Search" >
</mat-form-field>
On entering the input fieled by default the placeholder will go above.
How can i hide the placeholder on entering to the input field?
The simplest way of auto-hiding a placeholder text upon focus is using the onfocus and onblur events with the <input> element.
To move a placeholder:Click inside the placeholder. The border changes to slanted lines with eight sizing handles. Move the mouse pointer over the slanted lines. When a four-headed arrow appears over the borders, click and drag the placeholder to a new location.
I came into this question when looking for a way of hiding the placeholder when the input field is not empty, the solution I found is a modification of Krishna's, replacing the mat-form-field
floatLabel
argument from always
to never
:
<div class="example-container">
<mat-form-field
[floatLabel]="'never'">
<input matInput placeholder="Simple placeholder" required>
</mat-form-field>
</div>
You can try:
DEMO ----> Solution
You can also create Directive for same
You can replace (click) ----> (focus) as you need
<mat-form-field floatLabel=never>
<input (click)="checkPlaceHolder()" (blur)="checkPlaceHolder()" matInput placeholder=" {{myplaceHolder}} ">
</mat-form-field>
In TS:
myplaceHolder: string ='Enter Name'
checkPlaceHolder() {
if (this.myplaceHolder) {
this.myplaceHolder = null
return;
} else {
this.myplaceHolder = 'Enter Name'
return
}
}
you can try this solution.
use [floatLabel]="'always'"
I have create a demo on Stackblitz
<div class="example-container">
<mat-form-field
[floatLabel]="'always'">
<input matInput placeholder="Simple placeholder" required>
</mat-form-field>
</div>
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