Show value of option when chosen on select in Angular

John

I'm a newbie in Angular. I have a problem with select tag. I have a list country name and country code. When I chosen an option, I want the result on the select show only country code(value of option) instead of country name and country code. Example:

<select>
  <option value="+1">USA: +1</option>
  <option value="+66">THA: +66</option>
  <option value="+56">CHL: +56</option>
  <option value="+84">VN: +84</option>
</select>

When I select the USA, select tag will show "USA: +1" on screen, but I want to only show "+1" in this.

enter image description here

Have any solutions for this case? If yes, please send me any example code. Thanks so much!

ConnorsFan

In the markup, you can define a hidden first option with the default selected value, and set a handler for the (change) event:

<select (change)="onSelectChange($event)">
  <option hidden value="+1">+1</option>
  <option value="+1">USA: +1</option>
  <option value="+66">THA: +66</option>
  <option value="+56">CHL: +56</option>
  <option value="+84">VN: +84</option>
</select>

Every time an option is selected:

  • The value of the hidden option is set to the selected value
  • The text of the hidden option is set to the selected value
  • The hidden option is selected
onSelectChange(event: Event) {
  let select = event.target as HTMLSelectElement;
  select.options[0].value = select.value;
  select.options[0].text = select.value;
  select.selectedIndex = 0;
}

See this stackblitz for a demo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Angular Material: Prevent layout resizing when select box value is chosen

How to hide/show a div id with jQuery when html option select is chosen

Show Material UI Select (Drop Down) conditionally based on option chosen

Unable to show HTML select option initially when starting the page in Angular

Vue JS how to change value of varaible on chosen select's option

Show value of select option in html

Jquery callback when any select box option is chosen (not changed)

How to capture event when allowClear option of select2 is chosen?

How to avoid Material UI Select focus when option is chosen?

How to display radioboxes or textboxes when an option is chosen in a select tag?

Angular: Show value in a select

Angular Form Builder - set value based on chosen select value property

Get current value when change select option - Angular2

<select> to inherit the styles of the chosen <option>

Datepicker to show different value in dropdown when a specific day is chosen

Get the value of a select option in Angular

show option value selected in form select

Show Value Instead of text in select option

Show button when select option and checkbox is checked

show a label when hovered in a particular option in select

How to show table with data related to some ID when I select option with value of that ID?

Radio Buttons/Checkboxes/select option- Adding price(value) total of chosen item, and also adding them in an array

jQuery trigger click (or select) on chosen dropdown option

How to select the same option that was chosen in another page

HTML SELECT resizes depending on chosen option

How to set only one option in jquery chosen multi select when I submit the form

rails 3 collection with chosen-select how to show the right value on edit

Angular 2, set default value to select option

Angular - Disable select option if value exists in array