I have encountered a situation where the select options are non-unique, with the same value representing different things. This is how our data is structured and I need to work within those constraints.
<select id="mySelect">
<option value = "1">Apple</option>
<option value = "2">Orange</option>
<option value = "3">Pineapple</option>
<option value = "1">Banana</option>
</select>
In order to set the value based on index, I can use the selectedIndex property.
document.getElementById("mySelect").selectedIndex = 3;
However, when using Angular 2, I encounter an error stating "Property 'selectedIndex' does not exist on type 'HTMLElement'. any".
How can I retrieve selectedIndex in Angular 2 or find a workaround for this issue?
Update: I am attempting to set a default value for the select option based on other data retrieved from the database. The scenario is as follows:
this.data$.subscribe(data => {
this.data = data;
if (data.someConditionMet) {
document.getElementById("mySelect").selectedIndex = 3;
}
},
error => this.errorMessage = <any>error);
It is important to note that while the values in the options may be duplicate, the entries themselves are unique. For example, value "1" could represent either "Apple" or "Banana". This is why I prefer using index over value.