0

i'm facing an issue with the lightning-combobox standard comoponent. I can't achieve to change it's value programmatically.

I use the following code :

Markup :

<lightning-combobox
class="start-select"
label="Select Start"
options={startOptions}
onchange={handleOvertimeStartChange} ></lightning-combobox>

And my code to change to value :

const startSelect = this.template.querySelector(".start-select");
if (startSelect) {
    startSelect.value = this.startValue;
}

but the selected value of the combobox doesn't change on the UI.

Any clue about that ?

Thanks in advance

1

If startValue does not have a value which is one of the options in startOptions, then it will not work.

You can try below in your component which will work:

HTML:

<template>
    <lightning-button label="Change Select" onclick={changeSelect}></lightning-button>
    <lightning-combobox
                        class="start-select"
                        label="Select Start"
                        options={startOptions}
                        onchange={handleOvertimeStartChange}></lightning-combobox>
</template>

JS:

import { LightningElement, wire, api, track } from 'lwc';
import Parent from 'c/parent';

export default class Poc extends Parent {
    @track
    startOptions = [
        { label: 'New', value: 'new' },
        { label: 'In Progress', value: 'inProgress' },
        { label: 'Finished', value: 'finished' }
    ];

    changeSelect() {
        const startSelect = this.template.querySelector('.start-select');
        if (startSelect) {
            startSelect.value = 'inProgress';
        }
    }
}

Notice that inProgress is one of the options so it will work.

1
  • 2
    Ok thank you for your answer, it pointed me te solution. I was passing a value which was present in startOptions but it wasn't of the right type. I was passing passing a number while the value in startOptions was a String ! – Ombrelin Aug 21 '19 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.