Reputation: 21
I need to create another Select2 element but when I create the new one the previous one doesn't work.
function make(){
$('.agregar_unidad').each(function(index) {
if ($(this).html().length!=27) {
//$(this).children('select').select2("destroy");
} else {
$(this).html('<select class="itemNameUnidad" form-control id="nombre_Unidad_Ejecutora" name="itemName"></select>');
}
$(this).find('select').select2({
placeholder: '--- Seleccione la unidad ---',
alowClear:true,
ajax: {
url: '<?php echo base_url(); ?>home/getUnidad',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
});
}
Upvotes: 0
Views: 65
Reputation: 215
Because you are initializing all your select element in your DOM. That's why it is affecting the first select with the settings you initialized to it.
Try to initialize your script to:
$('.agregar_unidad').closest('select').select2({settings})
Upvotes: 1
Reputation: 21
var nombre = 0;
function refresh(){
$( ".agregar_unidad" ).last().html('<select class="itemNameUnidad" form-control id="nombre_Unidad_Ejecutora'+nombre+'" name="itemName"></select>');
nombre++;
$('.itemNameUnidad').last().select2({
placeholder: '--- Seleccione la unidad ---',
alowClear:true,
ajax: {
url: '<?php echo base_url(); ?>home/getUnidad',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
}
At the end this works for me, this is the solution a made, but if you can tell me how do it better it would be nice cause Im just a student, thanks for the help...
Upvotes: 0