1

I have a jquery dropdown which looks as follows after inspecting it in DOM explorer.

<select name="cityID" id="ddlCity" style="width: 128px; display:none;" data-val="true" data-val-number="ID must be a number">
    <option value = "0">Select city</option>
    <option value = "1234">New York</option>
    <input id="txt_ddlCity" style="width: 100px;" autocomplete="off"></input>
</select>

I want to set the value of this dropdown to 'New York' by using index.

I tried

 $("select#ddlCity").prop('selectedIndex', 1);

However, the value is not getting selected in dropdown.
I also tried to set using value as $("#ddlCity").val(1234); but it also doesn't work.

Can anyone figure out what I am doing wrong here. any help would be greatly appreciated.

Update:
This is not the duplicate of mentioned question. None of the answer is working for my case. Also please note it contains inside tag. Not sure if it is making any issue

  • `$("#ddlCity option").eq(0).prop('selected','selected');` – guradio Mar 27 '18 at 05:32
  • You have omitted an opening double-quote at the start of your `style` attribute value for `select#cityID`. That might not get parsed nicely in certain browsers. – Alexander Nied Mar 27 '18 at 05:32
  • its depend on Jquery version prop or attr – Jehad Ahmad Jaghoub Mar 27 '18 at 05:33
  • Please remove duplicate tag. None of the solution is working for me. Also please note it contains inside tag. Not sure if it is making any issue. –  Mar 27 '18 at 05:55
  • If this is not a duplicate, you should demonstrate that fact with a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Show your [research](https://meta.stackoverflow.com/q/261592) in the body of your question. The accepted answer of the other question worked for nearly 1000 users. Most likely you are doing something wrong that you *didn't* include in the question (and you should probably ask a different one and include all of the code necessary to replicate the problem). – NightOwl888 Mar 27 '18 at 12:26

0 Answers0