add default value to webform input elements (primitive or function)

This commit is contained in:
Anton Anders 2017-07-07 09:20:33 +02:00
parent 86b12c43c6
commit 3ff6aafcb2

View file

@ -73,6 +73,7 @@ $(function() {
name: 'name', name: 'name',
tag: 'input', tag: 'input',
type: 'text', type: 'text',
value: '',
placeholder: 'Your Name', placeholder: 'Your Name',
}, },
{ {
@ -80,6 +81,7 @@ $(function() {
name: 'email', name: 'email',
tag: 'input', tag: 'input',
type: 'email', type: 'email',
value: '',
placeholder: 'Your Email', placeholder: 'Your Email',
}, },
{ {
@ -87,6 +89,7 @@ $(function() {
name: 'body', name: 'body',
tag: 'textarea', tag: 'textarea',
placeholder: 'Your Message...', placeholder: 'Your Message...',
value: '',
rows: 7, rows: 7,
}, },
], ],
@ -361,12 +364,13 @@ $(function() {
} }
$.each(this.options.attributes, function(index, value) { $.each(this.options.attributes, function(index, value) {
var item = $('<div class="form-group"><label>' + _this.T(value.display) + '</label></div>') var item = $('<div class="form-group"><label>' + _this.T(value.display) + '</label></div>')
var _defaultValue = (typeof value.value === 'function') ? value.value() : value.value;
for (var i=0; i < (value.repeat ? value.repeat : 1); i++) { for (var i=0; i < (value.repeat ? value.repeat : 1); i++) {
if (value.tag == 'input') { if (value.tag == 'input') {
item.append('<input class="form-control" name="' + value.name + '" type="' + value.type + '" placeholder="' + _this.T(value.placeholder) + '">') item.append('<input class="form-control" name="' + value.name + '" type="' + value.type + '" placeholder="' + _this.T(value.placeholder) + '" value="' + (_defaultValue || '') + '">')
} }
else if (value.tag == 'textarea') { else if (value.tag == 'textarea') {
item.append('<textarea class="form-control" name="' + value.name + '" placeholder="' + _this.T(value.placeholder) + '" rows="' + value.rows + '"></textarea>') item.append('<textarea class="form-control" name="' + value.name + '" placeholder="' + _this.T(value.placeholder) + '" rows="' + value.rows + '">' + (_defaultValue || '') + '</textarea>')
} }
} }
$form.append(item) $form.append(item)