Issue with selecting value from dropdown menu

Viewed 30

I am trying to pick a default value from dropdown and disable it to user not change it with bellow code,

function brandPickerHn() {
        $("#Search__32Page__32Layout__46dropdownlist5 option[value='Value2']").attr("selected", "selected");
        $("#Search__32Page__32Layout__46dropdownlist5").attr("disabled", true);
    }

Issue here is that, dropdown is mandatory and I can not move forward because even tough I used,

.attr("selected", "selected")

.prop("selected", true)

.click()

.change()

I can see the selected option in dropdown but like I didn't set it so still asking me to go and select any value from dropdown. If I not use code for disabling Dropdown there is no problem.

Can you please assist, where I am making mistake ? Please not tell me to see older posts as I already dig whole site but nothing working found, thats why asking it.

Thank you in advance.

1 Answers

You need to set the value of the select element directly, as this is where the value is stored:

function brandPickerHn() {
    $("#Search__32Page__32Layout__46dropdownlist5").val('Value2');
    $("#Search__32Page__32Layout__46dropdownlist5").attr("disabled", true);
}
Related