Specify default value for form field?
-
Is there any way to specify the default value for a form field, either at the field level or the form level? I can't seem to find documentation regarding this, but it would be a helpful feature for advanced registration forms.
-
@elliotdickison I don't know of a way to get the default value for a form, but you may be able to use javascript to populate the values on the form.
-
I think you can try using custom JavaScript code. To set a default value for a form field, you can utilize JavaScript to manipulate the field's value after the form is loaded. Here's an example of how you can accomplish this:
Add an HTML class or ID to the form field you want to set a default value for. For example, let's say the class is "default-value-field".
In your FusionAuth theme or custom HTML file, add a script tag that runs after the form has loaded:
html <script> document.addEventListener("DOMContentLoaded", function() { // Find the form field by class or ID var defaultField = document.querySelector(".default-value-field"); // Set the default value defaultField.value = "Your default value"; }); </script>
This script listens for the "DOMContentLoaded" event, which occurs when the page is fully loaded. It then selects the form field using the class or ID you specified and sets its value to the desired default value.
-
Thanks for the tips! I do love that customizing FusionAuth's theme templates allows you to accomplish pretty much anything. For our use-case - lots of custom forms for lots of tenants - I would prefer to be able to use the advanced registration form feature instead of polluting theme files with JS snippets. At that point I think I might as well hardcode the HTML for the fields instead of adding JS.
-
@elliotdickison Hi, Did you find a way to provide default values to a form field? We're also not looking forward to add HTML for each of the theme. Thanks in advance.
-
@vatsal We did not