How can I set the selected item in a select after adding a record?

I have a select (dynamic) with a button to add items to it. Clicking the button opens a modal which allows a record to be added. I then have an on success action to close the modal and reload the data so the select shows the new record. I have also added an action to Set Selected Index to the identity returned by the API but it’s not working.

Again, I suspect I’m doing it wrong. Initially I tried Set Value but that didn’t work either.

Here’s my code:

<form is="dmx-serverconnect-form" id="serverconnectformAddEngineer" method="post" action="dmxConnect/api/Jobs/AddEngineer.php" dmx-generator="bootstrap5" dmx-form-type="horizontal" dmx-on:success="modalAddEngineer.hide();connDropdownEngineers.load({});modalAddEditJob.serverconnectformAddEditJob.inp_jobEngineerID.setSelectedIndex(modalAddEngineer.serverconnectformAddEngineer.data.insertEngineer.identity)">

The API is returning the identity correctly (confirmed in the dev console).

Hi.
Indexes are 0,1,2,3. They are different from IDs.
Set value is what should work, provided you have set the “optionvalue” of select to the same field as identity from insert.
If set value does not work, it could be because of number & string comparision. Try changing the identity to number or string and see if that works.

Thanks Sid.

I did think Set Value was the right method so have gone back to that but still no joy. I’ve also set the identity to convert to a number but still not working.

UPDATE: Now working. I had the Selected Value set to my Data Detail (the form is also an edit form) so that must have been overriding it. So I changed that to use the identity returned but added a default value of the Data Detail so now it should try the identity first and then use the existing value if it’s not there.

Thanks for your steer on this.

1 Like