To quote @DanSingerman ,
I know setting it by value is pretty trivial. e.g.
$("#my-select").val(myVal);
Referring to this previously asked similar question:
I am trying to set the SELECTed OPTION via
$("#" + fragment + ".gender").val('').prop('disabled', false);
resp
$("#" + fragment + ".gender").val(gender).prop('disabled', 'disabled');
but nothing happens. Using [name= doesn't work either.
The select (in a fragment personInstance(participant)):
<select class="custom-select custom-select" th:id="__${personInstance}__.gender"
th:name="__${personInstance}__.gender">
<option value="" selected
th:attr="data-salid=''"
th:text="#{option.select}">Select
</option>
<option th:each="salIter : ${salutations}"
th:attr="data-salid='__${salIter.baseIdentity.id}__'"
th:value="${{salIter.gender}}"
th:inline=text> [[#{${'gender.' + salIter.gender}}]]
</option>
</select>
Code:
$("select[name$='.salutationId']").change(function (event) {
/* propagate salutation gender to gender field and enable/disable */
var fragment = this.name.match(/^(\w+)\..*$/);
var salid = $(this).val(); //JS: this.options[this.selectedIndex].value;
var gender = this.options[this.selectedIndex].getAttribute('data-gender');
console.log("sal/Frag = " + fragment[1] + "SX:" + this.selectedIndex + " val:-" + salid + "- GND:" + gender + " set:" + fragment[1] + ".gender");
if (salid === '') {
$('[name="' + fragment[1] + '.gender"]').val('').prop('disabled', false);
} else {
$('[name="' + fragment[1] + '.gender"]').val(gender).prop('disabled', 'disabled');
}
});
Generated HTML:
<select class="custom-select form-control" id="participant.salutationId"
name="participant.salutationId">
<option value="" selected>-Auswählen-</option>
<option value="1"
data-gender="MALE">Herr</option>
<option value="2"
data-gender="FEMALE">Frau</option>
<option value="3"
data-gender="DIVERS">Divers</option>
</select>
and
<select class="custom-select form-control" id="participant.gender"
name="participant.gender">
<option value="" selected>-Auswählen-</option>
<option value="MALE"
data-salid="1"> Männlich
</option>
<option value="FEMALE"
data-salid="2"> Weiblich
</option>
<option value="DIVERS"
data-salid="3"> Divers
</option>
</select>
Gender is an ENUM and mapped to externalized text. Using Jquery 3.4.1
Anything special with SELECTs again?