-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-tag-builder.js
86 lines (78 loc) · 1.71 KB
/
html-tag-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* helper for building html tag
* @param el
* @returns
*/
function element(el) {
this.elementText = el;
this.el = "<"+el;
this.startTagClosed = false;
/**
* close the html tag
* once tag is closed, then its not available for any more edits
*/
this.close = function() {
if (!this.startTagClosed) {
this.el += "></"+this.elementText+">";
} else {
this.el += "</"+this.elementText+">";
}
return this;
};
/**
* return html tag as String.
* typically once you are done with building tag and closed it
* you will do yield() to get the tag`s String representation
*/
this.yield = function () {
return this.el;
};
/**
* add `id` attribute to tag
*/
this.addId = function(id) {
this.el += " id='" +id+"'";
return this;
};
/**
* add `name` attribute to tag
*/
this.addName = function(name) {
this.el += " name='" +name + "'";
return this;
};
/**
* add `maxlenght` attribute to tag
*/
this.addMaxLength = function(maxLength) {
this.el += " maxlength='" + maxlength +"'";
return this;
};
/**
* add any arbitrary attribute to tag, `style` etc
*/
this.addAttrib = function(attrib, value) {
this.el += " " + attrib + "='" + value +"'";
return this;
};
/**
* add value attribute to tag
*/
this.addValue = function(value) {
this.el += " value='" + value + "'";
return this;
};
/**
* add innerHTML to tag,
* this is used for container tags like span, div etc
*/
this.addInnerHtml = function(innerHtml) {
if (this.startTagClosed) {
this.el += innerHtml;
} else {
this.el += ">"+innerHtml;
this.startTagClosed = true;
}
return this;
};
}