I am using Django Autocomplete Light for some fields in my form. I have the need to automatically set the value of the field when the user performs a specific action. For example, if the user checks a certain box in the form, I want the selected value of the autoselect field to change from the placeholder text to the value I need without the user having to also click the autoseelct and select the value themselves. For a standard dropdown or input, this would be accomplished by
$("#input-id").val("XYZ");
But this does not seem to work for autocomplete fields.
I tried making changes to the generated span tags that Django Autocomplete Light manages and can make it appear to select a value:
$("#select2-input-id-container").attr("title", "XYZ").text("XYZ")
"XYZ" now appears in the field, but when the form is submitted, it thinks nothing has been selected and submits null (or if the field is required, it asks the user to select a value).
Is there a way to do this?
EDIT: Part of the group of HTML tags Autocomplete Light manages is a select tag like the following:
<select name="ac_options" data-placeholder="Choose Option..." tabindex="-1" class="modelselect2 form-control select2-hidden-accessible" required="" id="input_id" data-autocomplete-light-language="en" data-autocomplete-light-url="/options-autocomp/" data-autocomplete-light-function="select2" data-select2-id="input_id" aria-hidden="true">
<option value="" selected="" data-select2-id="1">---------</option>
<option value="An Option" data-select2-id="20">An Option</option>
<option value="XYZ" data-select2-id="26">XYZ</option>
</select>
There is a data-select2-id that seems to have values internally managed by Autocomplete Light - if there is any place these id numbers and the options to which they are associated are exposed, adding the options to the select tag with the right data-select2-id might do the trick. The number (26 in the example above for "XYZ") is not the primary key of "XYZ" in the options table in the database, it is created by AutoComplete Light.
I also tried a combination of the setting the value of the select as well as the span - The form still thinks there is nothing selected in the autocomplete.
Ideally, though, instead of trying to hack how Django Autocomplete Light manages it's many tags, it would be nice to have a way to just set the value of the field similarly to any other input field.
For reference, here is the entire structure Autocomplete Light creates in HTML when a field is declared as autocomplete:
<select name="input" data-placeholder="Choose Option..." tabindex="-1" class="modelselect2 form-control select2-hidden-accessible" required="" id="id_input" data-autocomplete-light-language="en" data-autocomplete-light-url="/input-autocomp/" data-autocomplete-light-function="select2" data-select2-id="id_input" aria-hidden="true">
<option value="" selected="" data-select2-id="1">---------</option>
<option value="An Option" data-select2-id="20">An Option</option>
<option value="XYZ" data-select2-id="26">XYZ</option>
</select>
<span class="select2 select2-container select2-container--default select2-container--focus" dir="ltr" data-select2-id="2" style="width: 280px;">
<span class="selection">
<span class="select2-selection select2-selection--single modelselect2 form-control" role="combobox" aria-haspopup="true" aria-expanded="false" tabindex="3" aria-labelledby="select2-id_input-container">
<span class="select2-selection__rendered" id="select2-id_input-container" role="textbox" aria-readonly="true">
<span class="select2-selection__placeholder">Choose Option...</span>
</span>
<span class="select2-selection__arrow" role="presentation">
<b role="presentation"></b>
</span>
</span>
</span>
<span class="dropdown-wrapper" aria-hidden="true"></span>
</span>