-
Notifications
You must be signed in to change notification settings - Fork 0
/
Color Preview.js
54 lines (44 loc) · 1.34 KB
/
Color Preview.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
console.log('init');
// init add preview & event listener for input/change
$(".modal-body input[type=text]").each(function(index, el) {
updateColor(el);
}).on('input', function(e) {
updateColor(this);
});
function updateColor(el) {
var jEl = $(el);
var parent = jEl.parent();
if (jEl.val().indexOf("#") === 0)
{
var colorPreview = $(".colorPreview", parent);
// update
if (colorPreview.length > 0)
{
var input = $('input[type=text]', parent);
var color = input.val();
colorPreview.css('background-color', color);
}
// add
else
{
addColorPreview(el)
}
}
else // try to remove
{
$(".colorPreview", parent).remove();
}
}
function addColorPreview(el) {
var jEl = $(el);
var parent = jEl.parent();
var input = $('input[type=text]', parent);
var topPos = input.position().top;
var color = input.val();
// set parent's positioning
parent.css("position", "relative");
// add our color preview
$("<div class='colorPreview' style='display: block; width: 27px; height: 27px;"+
" position: absolute; top: 24px; right: 0px; border: 1px solid black; "+
" background-color: "+color+"'></div>").appendTo(parent);
}