0

I have this select

  <select id="AttorneyEmpresa" class="form-control">
    <option value="40765167-7972-483f-b34d-6852bb3e7ae8">A</option>
    <option value="EF6620EA-8411-444B-95DE-93C92C864476">B</option>
    <option value="e300eaf7-951e-44ef-9a09-94ce1ab41bea">C</option>
    <option value="22D4ED6E-4AF9-476B-BF04-3B4B84565BDD"D</option>
    <option value="F67E520F-C176-4D6D-B537-04970B82ACE9"E</option>
  </select>

I have an ajax call and on success I am trying to set the selected value, using my data.IdSelected, which is a guid

success: function (data) {
  // brings the data.SelectedId saved in database
  $("#AttorneyEmpresa select").val(data.IdSelected);
}

However it seems the first option the selector is bringing, is the option A always.

What is wrong with the code?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
j d
  • 3
  • 2
  • You just need `$("#AttorneyEmpresa").val(data.IdSelected);` – Steve Jul 02 '19 at 10:56
  • If you go to the console and do `$("#AttorneyEmpresa").length` you will see 1 (meaning the selector has found it) More than likely `$("#AttorneyEmpresa select").length` will return 0. – Steve Jul 02 '19 at 10:58
  • Also make sure the data.IdSelected === the option value and not `{e300eaf7-951e-44ef-9a09-94ce1ab41bea}` or something – mplungjan Jul 02 '19 at 11:03
  • _This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting._ – mplungjan Jul 02 '19 at 11:03

1 Answers1

0

try this code

success: function (data) {
  // brings the data.SelectedId saved in database
  $("#AttorneyEmpresa select").val(data.IdSelected).change();
}

JQuery - how to select dropdown item based on value

Abdo-Host
  • 2,470
  • 34
  • 33