A small jQuery based tool to generate HTML5 input controls from a plain options object.
HTMLControlGenerator({
inputType: "text",
parent: "form-row",
id: "user-name",
class: "form-field",
onchange() { console.log("changed"); }
});- jQuery 4.0.0
- A browser with ES2020 support (every current Chrome, Edge, Firefox and Safari)
Load jQuery first, then the library. The addon is optional.
<script src="https://code.jquery.com/jquery-4.0.0.min.js" defer></script>
<script src="src/HTMLControlGenerator.min.js" defer></script>
<script src="src/addons/hungarianInputControls.min.js" defer></script>The readable sources are next to the minified files, src/HTMLControlGenerator.js and src/addons/hungarianInputControls.js.
| Kind | inputType |
|---|---|
| Text | text, password, email, search, tel, url |
| Number | number, range |
| Date and time | date, datetime-local, month, time, week |
| Choice | radio, checkbox, select, color, file |
| Button | button, submit, reset |
| List | ul, ol |
| Hungarian addon | hunPIN, hunCRID, hunTID |
HTMLControlGenerator({
inputType: TYPE,
parent: ID_OF_PARENT_ELEMENT,
id: ID_OF_GENERATED_ELEMENT,
class: CLASS_OF_GENERATED_ELEMENT,
name: NAME_OF_THE_ELEMENT,
value: VALUE_OF_THE_ELEMENT,
min: MIN_VALUE_OF_THE_ELEMENT,
max: MAX_VALUE_OF_THE_ELEMENT,
checked: true,
readOnly: true,
disabled: true,
items: [{ value: VALUE, text: TEXT, selected: true }],
data: PASSED_TO_THE_HANDLERS,
onchange: function (data) { SOME_DEFINITION },
onclick: function (data) { SOME_DEFINITION }
});| Option | Applies to | Description |
|---|---|---|
inputType |
all | The control to generate, required. |
parent |
all | Id of the element the control is appended to, required. If no element has this id the control is appended to the body in a new div. |
id, class |
all | Id and class of the generated element. |
name, readOnly, disabled |
inputs and select |
Set on the generated element. |
value |
inputs except reset and file |
Value of the element. |
min, max |
date, number, range |
Limits of the element. |
checked |
radio, checkbox |
Checked state. |
items |
select, ul, ol |
The options or list items, see below. |
data |
all | Passed to the handlers as the first argument. |
onchange, onclick |
all | Event handlers. change is accepted as an alias of onchange. |
For a select each item is { value, text, selected }.
For ul and ol each item is { id, class, text, data, onclick, onchange }. An item without a handler uses the handler of the list.
HTMLControlGenerator({
inputType: "ul",
parent: "menu",
onclick() { console.log("some item"); },
items: [
{ text: "First" },
{ text: "Second", data: 2, onclick(data) { console.log("item", data); } }
]
});HTMLControlGenerator.create(options) builds the same control and returns it as a jQuery object instead of appending it to the parent.
const input = HTMLControlGenerator.create({ inputType: "number", min: 0, max: 10 });HTMLControlGenerator.register(inputType, factory) adds a new input type. The factory gets the options and returns the element to append.
HTMLControlGenerator.register("percent", (options) =>
HTMLControlGenerator.create({ ...options, inputType: "number", min: 0, max: 100 })
);src/addons/hungarianInputControls.js adds three controls made of number fields with spacers between them. The value is the number as one string of digits, inputClass is the class of the number fields.
inputType |
Control | Digits per field |
|---|---|---|
hunPIN |
Personal Insurance Number | 3 - 3 - 3 |
hunCRID |
Company Registration ID | 2 - 2 - 6 |
hunTID |
TAX Number | 8 - 1 - 2 |
HTMLControlGenerator({
inputType: "hunTID",
parent: "tax",
id: "tax-number",
inputClass: "tax-field",
value: "12345678901"
});An error is written to the console and shown in a div appended to the body.
| Code | Meaning |
|---|---|
| 0 | options or options.parent is undefined |
| 1 | Unknown inputType |
| 2 | No parent element and no body to append to |
Open example/index.html in a browser, it generates every control.
- Csaba Tóth - Initial work - csabatibortoth
- Page: - https://csaba.toth.today/projects/htmlcontrolgenerator/
This project is licensed under the MIT License - see the LICENSE file for details
- Rewritten in modern JavaScript (ES2020)
- jQuery updated to 4.0.0
- Only
HTMLControlGeneratoris global, the helper functions are private - Addons plug in with
HTMLControlGenerator.register() - An item of
ulandoluses its own handler, otherwise the handler of the list
HTMLControlGenerator.create()andHTMLControlGenerator.register()changeas an alias ofonchange
olis generatedtel,date,emailandtimeare generated in every browser
- Browser detection and the unsupported browser error (code 999)
errorLinefrom the error objects
- Fixes
- Fix
- Minified versions
- Addons
- Support for hungarian TAX Number
- Support for hungarian Personal Number
- Support for hungarian Company Registration Number
- Version in header
- Refactoring base code
<ul>and<ol>example
- Core input generation