You need to wait until jQuery and select2.js
have loaded:
function formatValues(data) {
return data.ime_prezime;
}
$(document).ready(function() { // add this
$('#parcele').select2({
ajax: {
dataType: "json",
url: "json.php",
results: function (data) {
return {results: data};
}
},
width: "300px",
formatResult: formatValues,
formatSelection: formatValues,
multiple: true
});
}); // add this
EDIT: I found your problems:
You don't have any elements with
id
"parcele" on your actual page at http://agroagro.com/template/tema/zadaci.html# - I think you are thinking of the elements withid
"parcela" (notice the "a" instead of an "e").You actually have two elements with the
id
"parcela", but HTML ids have to be unique.
To fix this: Rename one of the elements with the id
"parcela", then use one of them where you have "parcele" in your existing JavaScript.
Also, just to verify that everything works if you fix the naming problems, I created this jsFiddle, which works correctly.