How to: Set default values for required column in a SharePoint 2016 newform.aspx using javascript
-
26-01-2021 - |
Frage
I have a simple list with a single line of text column. I have a JS function that reads data from other list an set default value for this column in the new form. I simply use.
$(“select[Title=’Fieldname’]”).val(‘Value’);
All works well, if I change my column type and set "Require that this column contains information" to true, the code no longer works. Anyone knows why and how I can get around this issue?
Lösung
When you make the the field required it changes the title attribute of field element . So the selector you are using does not return any element.
You can see the title of the field by inspecting respective textbox/select element on new form in developer tool.
Try using below selectors:
Using Title property:
$("select[title='Fieldname Required Field']").val("Value");
Here Fieldname is display name of field.
Using Id property:
Using Id for element selector is bit tricky in SharePoint as it appends unnecessary string in id attribute by default.
$("select[id^='Fieldname']").val("Value");
^
denotes that id starts with
your Fieldname(Internal name of field).