瀏覽代碼

Now generating text input fields

Brendan Abolivier 8 年之前
父節點
當前提交
078c4cbb79
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 ID: 8EF1500759F70623
共有 1 個文件被更改,包括 69 次插入0 次删除
  1. 69
    0
      form.js

+ 69
- 0
form.js 查看文件

@@ -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
+}