I am trying to select the second option form a dropdown if specific conditions are met, am having problems with the selectedIndex
<select id="contact" on-change="selectContact">
<option value="-1" selected>Select Contact</option>
<template is="dom-repeat" items="[[contacts]]" as="contact" index-as="index">
<option value="[[contact]]">[[contact]]</option>
</template>
</select>
<select id="name" on-change="selectName">
<option value="-1" selected>Select Name</option>
<template is="dom-repeat" items="[[names]]" as="name">
<option value="[[name]]">[[name]]</option>
</template>
</select>
...
selectContact() {
for (let i = 0; i < this.customerTable[0].length; i++) {
if(true) {
array[i] = this.customerTable[0][i]['name'];
}
}
this.names = this.$.util.utilFn(array);
if(this.names.length == 1) {
this.shadowRoot.querySelector('#name').selectedIndex = 2;
}
}
How can I select the second child of a dom-repeat dropdown?
In selectContact()
, you're setting this.names
(to which the second <dom-repeat>.items
is bound) and immediately attempting to select the first item of that <dom-repeat>
before the DOM has actually updated.
You actually need to wait until the next render frame with Polymer.RenderStatus.afterNextRender()
before selecting the item:
selectContact() {
this.names = ['John'];
if (this.names.length === 1) {
Polymer.RenderStatus.afterNextRender(this, () => {
this.shadowRoot.querySelector('#name').selectedIndex = 1;
});
}
}