Sfoglia il codice sorgente

Now generating text input fields

Brendan Abolivier 8 anni fa
parent
commit
078c4cbb79
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 ha cambiato i file con 69 aggiunte e 0 eliminazioni
  1. 69
    0
      form.js

+ 69
- 0
form.js Vedi File

@@ -0,0 +1,69 @@
1
+function generateForm(id) {
2
+    var el = document.getElementById(id);
3
+    
4
+    var input = {
5
+        name: getField('form_name', 'Your name', false), // TODO: configurable prefix
6
+        addr: getField('form_addr', 'Your e-mail address', true),
7
+        subj: getField('form_subj', 'Your mail\'s subject', false),
8
+        text: {}
9
+    };
10
+    
11
+    // Adding nodes to document
12
+    
13
+    el.appendChild(input.name);
14
+    el.appendChild(input.addr);
15
+    el.appendChild(input.subj);
16
+}
17
+
18
+
19
+// Returns a form field
20
+// id: field HTML identifier
21
+// placeholder: placeholder text
22
+// email: boolean: is it an email field?
23
+// return: a div node containing a label and an input text field
24
+function getField(id, placeholder, email) {
25
+    var field = document.createElement('div');
26
+    
27
+    field.setAttribute('id', id); // TODO: configurable prefix
28
+    field.appendChild(getLabel(id, placeholder));
29
+    field.appendChild(getInputField(id, placeholder, email));
30
+    
31
+    return field;
32
+}
33
+
34
+
35
+// Returns a label
36
+// id: field HTML identifier
37
+// content: label's inner content
38
+// return: a label node the field's description
39
+function getLabel(id, content) {
40
+    var label = document.createElement('label');
41
+    
42
+    label.setAttribute('for', id + '_input');
43
+    label.innerHTML = content;
44
+    
45
+    return label;
46
+}
47
+
48
+
49
+// Returns an input text field
50
+// id: field HTML identifier
51
+// placeholder: placeholder text, field description
52
+// email: boolean: is it an email field?
53
+// return: an input text or email field (depending on "email"'s value') with an
54
+//         HTML id and a placeholder text
55
+function getInputField(id, placeholder, email) {
56
+    var input = document.createElement('input');
57
+    
58
+    if(email) {
59
+        input.setAttribute('type', 'mail');
60
+    } else {
61
+        input.setAttribute('type', 'text');
62
+    }
63
+    
64
+    input.setAttribute('required', 'required');
65
+    input.setAttribute('placeholder', placeholder);
66
+    input.setAttribute('id', id + '_input');
67
+    
68
+    return input;
69
+}