Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide placeholder onclick in material

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.

enter image description here

How can i hide the placeholder on entering to the input field?

like image 257
Shankar Avatar asked Aug 09 '18 09:08

Shankar


People also ask

How do you hide the placeholder text of an input file?

The simplest way of auto-hiding a placeholder text upon focus is using the onfocus and onblur events with the <input> element.

How do I move my placeholder?

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.


3 Answers

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>
like image 161
Learning is a mess Avatar answered Oct 23 '22 00:10

Learning is a mess


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
    }
  }
like image 37
Akj Avatar answered Oct 22 '22 22:10

Akj


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>
like image 13
Krishna Rathore Avatar answered Oct 22 '22 23:10

Krishna Rathore