-
Notifications
You must be signed in to change notification settings - Fork 1
/
nefertiti.js
55 lines (49 loc) · 2.39 KB
/
nefertiti.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
var Nefertiti = {
init: function(o = {selector : '.select'}){
if (!o.hasOwnProperty('el')){
o.el = '.select';
}
class_name = o.hasOwnProperty('classes') ? o.classes : 'nefertiti';
var selects = document.querySelectorAll(o.el);
selects.forEach(function(select){
select.style.display = 'none';
_name = select.getAttribute('name');
_list_id = _name + '_id'
select_parent = select.parentElement;
// create element and list
var contentElement = document.createElement('input');
var dataListElement = document.createElement('datalist');
dataListElement.setAttribute('id', _list_id)
if (o.hasOwnProperty('classes') &&
o.classes instanceof Array &&
o.classes.length !== 0
){
o.classes.forEach(function(cls){
contentElement.classList.add(cls);
});
}
contentElement.setAttribute('placeholder', o.hasOwnProperty('placeholderText')? o.placeholderText : 'Start typing')
//contentElement.setAttribute('name', _name);
contentElement.setAttribute('type', 'text');
contentElement.setAttribute('list', _list_id)
l = select.options;
_kv_object = {}
for(i = 0; i<l.length; i++){
var option = document.createElement('option');
option.value = l[i].label;
option.innerText = l[i].value;
dataListElement.appendChild(option)
// build the key_value_object
_kv_object[l[i].label] = i; // define indexes here
//_kv_object[]
}
select_parent.appendChild(dataListElement);
select_parent.appendChild(contentElement);
// add event to handle change
contentElement.addEventListener('change', function(e){
// on change let set the current value to the select tag
select.selectedIndex = _kv_object[e.target.value]; // retrieve indexez
})
});
}
}