Help:Dynamic defaults for Page Forms

Summary

With a bit of Javascript and some additions to your form definitions you can set the content of form fields to any value you like, constant or generated. This even works, when the field is pre-filled.

Problem

Presently you can set defaults for form fields only to constant values (with some exceptions, granted). It is not possible to set the defaults to dynamic data, e.g. results from template calls or from. If there is preloaded data, you can not force it to default values, at all.

What do you have to do?
 * 1) Copy the javascript code from User:F.trott/dynamicSFDefaults.js to somewhere where it is executed, e.g. the MediaWiki:Common.js page of your wiki
 * 2) Include some of the following into your form definition:

default value

01 12 2345 12 34 56 AM GMT



<! -- for input types checkboxes, listbox --> name of 1st preselected value name of 2nd preselected value

Notes
 * 1) Tested (well, played with a bit) on IE6 and FF3.5.
 * 2) You always need the outer div with id="sfdefaults" set.
 * 3) An id can be used only once in any document, thus only one block of defaults is considered.
 * 4) The inner divs must have class="sfdefault".
 * 5) Checkboxes or listbox entries, which are already set, will never be unset by the script.
 * 6) For text, buttons, textarea and all date input types you can set class="sfdefault preserve"  to preserve values that are already set, e.g. from a preload. For date fields the preserve-setting from the day-component is used for all other components (month, year, ...) as well, i.e. there is no use in setting (or not setting) preserve for the month. You can not set preserve for radio buttons, checkbox, checkboxes or listbox.
 * 7) You can set the default value for "multiple" templates by including "[num]" between the template name and the field name, e.g. default value   . And yes, that is meant literally, you can not replace num by some number. I.e. you can not set different defaults for different incarnations of the multiple template.
 * 8) This tip works for the standard inputs, too. You can even set the text on buttons this way, if you so desire. Use the fieldnames below.

And if all that div-stuff is too unwieldy for you, then just stick it in a template. That's what this tip is all about, after all. ;-)