-
Notifications
You must be signed in to change notification settings - Fork 0
/
KClassTool.js
141 lines (79 loc) · 2.95 KB
/
KClassTool.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var kLib = kLib ||{} ;
(function(kLib)
{
kLib.addClassById = function(id,className)
{
var victim = document.getElementById(id);
addClasstoElement(victim,className);
}
function victimIsFound(victim)
{
if(victim) return true;
kLib.logError("note:"+ victimIsFound.caller +" could not find victim so exectution did not finish")
return false;
}
function addClasstoElement(victim,className)
{
if(!victimIsFound(victim)) return;
victim.className = victim.className + " "+ className;
}
kLib.addClasstoElement= addClasstoElement;
function addClassToManyElements(victims,className)
{
kLib.forEach(victims,function(element) {
addClasstoElement(element,className);
});
}
function removeClassToManyElements(victims,className)
{
kLib.forEach(victims,function(element)
{
kLib.log(element);
removeClassToElement(element,className) ;
});
}
function removeClassToElement(victim,className)
{
victim.className = victim.className .replace(className, "");
}
kLib.removeClassToElement = removeClassToElement;
kLib.doThisToElementsWithClassName = function(className,doThis)
{
var elementsWithClassName = document.getElementsByClassName(className);
kLib.forEach(elementsWithClassName,doThis);
}
kLib.validateOnlyThisElementHasClassName = function(victimElement,className)
{
kLib.removeClassToAllWithClassName(className,className);
addClasstoElement(victimElement,className);
}
//this will set the first Element with the given class name the given value
kLib.setElementContentByClassName = function(className,value)
{
var nameHolder = document.getElementsByClassName(className)[0];
nameHolder.innerHTML = value;
}
kLib.addClassToAllWithClassName = function(victimClassName,className)
{
var victims = document.getElementsByClassName(victimClassName);
addClassToManyElements(victims,className);
}
kLib.removeClassToAllWithClassName = function(victimClassName,className)
{
var victims = document.getElementsByClassName(victimClassName);
removeClassToManyElements(victims,className);
}
kLib.getFirstFromClassName = function(className)
{
var results = document.getElementsByClassName(className);
if(!results.length) throw "No elements were found with:"+className;
return results[0];
}
kLib.getFirstChildWithClassName = function(parent,className)
{
if(!parent || !kLib.isElement(parent)) throw "cannot find element: parent is invalid";
var results = parent.getElementsByClassName(className);
if(!results.length) throw "No elements were found with:"+className;
return results[0];
}
})(kLib)