Skip to content

Commit dd93c35

Browse files
author
Tray2
committed
Rewrite of how it creates new input elements before and after, and add cloning of inputs
1 parent e61cabc commit dd93c35

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

helper.js

+37-13
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,44 @@ function newElement(elementType) {
120120
return document.createElement(elementType);
121121
}
122122

123-
function addInputFieldAfter(selector, type, name, value) {
123+
function createInput(attributes) {
124124
let input = newElement('input');
125-
input.type = type;
126-
input.name = name;
127-
input.value = value;
128-
getElement(selector).after(input);
129-
}
130-
131-
function addInputFieldBefore(selector, type, name, value) {
132-
let input = newElement('input');
133-
input.type = type;
134-
input.name = name;
135-
input.value = value;
136-
getElement(selector).before(input);
125+
if (attributes.name === undefined) throw 'Name attribute is mandatory';
126+
if (attributes.type === undefined) throw 'Type attribute is mandatory';
127+
Object.keys(attributes).forEach(attribute => {
128+
if (attribute === 'required') {
129+
input.required = true;
130+
} else if (attribute === 'readonly') {
131+
input.readOnly = true;
132+
} else if (attribute === 'disabled') {
133+
input.disabled = true;
134+
} else {
135+
input[attribute] = attributes[attribute];
136+
}
137+
});
138+
return input;
139+
}
140+
141+
function addInputFieldAfter(selector, attributes) {
142+
let element = createInput(attributes);
143+
getElement(selector).after(element);
144+
}
145+
146+
function addInputFieldBefore(selector, attributes) {
147+
let element = createInput(attributes);
148+
getElement(selector).after(element);
149+
}
150+
151+
152+
function cloneInput(selector) {
153+
let element = getElement(selector);
154+
if (element.nodeName !== 'INPUT') throw `Element is not an input: ${selector}`;
155+
let clone = element.cloneNode();
156+
if (element.id !== undefined) {
157+
clone.id = element.id + Date.now();
158+
}
159+
clone.value = '';
160+
element.after(clone);
137161
}
138162

139163
async function postRequest(url, parameters = {}, loading = true) {

0 commit comments

Comments
 (0)