0

I have following for loop. Which sees if the data value is in the dropdown if so it will show it as selected.

for (var i in data) {
    $("#optionDropdwon option:contains(data[i])").prop("selected", true);
    console.log(data[i])
}

It works fine when I replace data[i] in the :contains() with an actual string i.e "xyz" but when I replace it with data[i] nothing happens. Yet I can see console.log(data[i]) shows me the proper values. I searched Stackoverflow but couldn't find something like what I am doing.

Please let me know where I am making a mistake. Thanks

1 Answer 1

1

You must construct a proper selector for this to work. In your example, you have only the literal string data[i] in the selector, but not the actual value

$("#optionDropdwon option:contains(" + data[i] + ")").prop("selected", true);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.