-
Notifications
You must be signed in to change notification settings - Fork 0
/
comboBox.js
35 lines (32 loc) · 922 Bytes
/
comboBox.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
var comboBox = function(divContainer, name, options, events={}){
var $options
// Chequeamos si la entrada es un url o un arreglo
if(RegExp(/https?:\/{2}/).test(options)){
$.ajax({
url: options,
success: function(response){
$options = response
},
async: false
})
} else {
$options = options
}
var $cbo = $("<select>")
.attr("name", name)
.attr("id", name)
// Cargamos las opciones
Object.keys($options).map(value => {
var $option = $("<option>")
.attr("value", value.replace(/ /g, ""))
.html($options[value])
$cbo.append($option)
})
// Cargamos el evento on change
if (events["change"]){
$cbo.on("change", events["change"])
}
$("#" + divContainer).append($cbo)
addInputStyles(divContainer)
}
var cbo = new comboBox("testcbo", "test", "http://localhost:3000/cbo", {"change": function(){console.log($(this).val())}})