Search code examples
javascripthtmlcssangulardropdown

How to hide selected option text in select box, Angular 4


I have this drop-down list of ethnicity. What I want is when I select clear ethnicity it should not appear in select box, and like box should be empty. How can i do that?

  <select
        #ethnicity
        [(ngModel)]="patient.Ethnicity"
        [class.text-dimmed]="!patient.Ethnicity"
        name="ethnicity"
        id="ethnicity"
        class="form-control input-underline input-lg ml-0">
          <option [ngValue]="null" disabled> Select </option>
          <option [ngValue]="null">Clear ethnicity</option>
          <option  value="Alaska-Native">Alaska Native</option>
          <option value="American-Indian">American-Indian</option>
          <option value="Asian">Asian</option>
          <option value="African-Americans">Black or African American</option>
          <option value="Hispanic">Hispanic or Latino</option>
          <option value="Jewish-Ashkenazi">Jewish - Ashkenazi</option>
          <option value="Jewish-Other">Jewish - Other</option>
          <option value="Native-Hawaiians">Native Hawaiian or Other Pacific Islander </option>
          <option value="Other">Other </option>
          <option value="Unknown">Unknown </option>
          <option value="White-Caucasian">White or Caucasian</option>
      </select>

like when I select it, it appears like this

enter image description here

but what I want is,

enter image description here

here is my demo clear ethnicity should show in dropdown but when i select it it should not show in select box like i shared screen shots

What should I do to solve this situation ? Thank you.


Solution

  • You can handle change event of select tag like this.

    onChange(value) {
        if(value == 'Clear ethnicity'){
          this.Ethnicity = '';
    }
    
    <select
                #ethnicity
                [(ngModel)]="Ethnicity"
                [class.text-dimmed]="!Ethnicity"
                name="ethnicity" (change)="onChange($event.target.value)"
                id="ethnicity"
                class="form-control input-underline input-lg ml-0">
    

    Demo: https://stackblitz.com/edit/angular-clear-option-select