So i have downloaded select2 i have “installed it” by putting it into my folder and then loaded it on my site when i check the console (where i can see all of the scripts being loaded) i can see the file select2.js
I went to their documentation and copied it and added $("#e9").select2();
However when i load the page i get the following error:
JavaScript
x
5
1
TypeError: $( ).select2 is not a function
2
3
4
$("#e9").select2();
5
Have anyone else experianced anything like this?
Additional information here is my script:
JavaScript
1
125
125
1
jQuery(document).ready(function(){
2
var max_amount = parseFloat($('#max_amount').val());
3
$( "#item_amount" ).keyup(function() {
4
if($(this).val() > max_amount){
5
$(this).val( max_amount);
6
}
7
if( /D/.test($(this).val()) ){
8
alert('Må kun indeholde tal!');
9
$(this).val('');
10
}
11
if($(this).val()== '0'){
12
alert('Må ikke være 0!');
13
$(this).val('');
14
}
15
});
16
$("#e1").select2();
17
18
});
19
function addToBasket(){
20
var amount = $('#item_amount').val();
21
if(amount == ""){
22
amount = 1;
23
}
24
25
if(amount > 0){
26
$.ajax({
27
type: 'POST',
28
url: myBaseUrl + 'Products/addItemToBasket',
29
dataType: 'json',
30
data: {
31
id: window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1),
32
amount: amount
33
},
34
success: function (data) {
35
var urlToBasket = myBaseUrl+'Products/basket';
36
var newAmount = parseInt(amount)
37
var price = data[0]['Product']['pris'];
38
var id = data[0]['Product']['id'];
39
var dat = data;
40
var tmp_basket_html = $('#basket_amount').html();
41
if($('#basket_amount').html() !== " Tom"){
42
$('#shopping_table_body').append(
43
"<tr id='"+id+"'>" +
44
"<td class='image'>" +
45
""+
46
"</td>" +
47
"<td class='name'>" +
48
" "+data[0]['Product']['name'] +
49
"</td>"+
50
"<td class='quantity'>" +
51
"x "+amount +""+
52
"</td>"+
53
"<td class='total'>" +
54
""+price*amount+
55
"</td>" +
56
""+
57
"<td class='remove'>" +
58
"<input class='icon-remove' type='button' onclick='removeItemFromBasket("+id+")'>"+
59
"</td>"+
60
"</tr>"
61
);
62
}else{
63
$("#shopping_menu").append(
64
"<ul class='dropdown-menu topcartopen'>"+
65
"<li id='basket_list'>"+
66
"<table id='shopping_table'>"+
67
"<tbody id='shopping_table_body'>"+
68
"<tr id='"+id+"'>" +
69
"<td class='image'>" +
70
""+
71
"</td>" +
72
"<td class='name'>" +
73
" "+data[0]['Product']['name'] +
74
"</td>"+
75
"<td class='quantity'>" +
76
"x "+amount +""+
77
"</td>"+
78
"<td class='total'>" +
79
""+price*amount+
80
"</td>" +
81
""+
82
"<td class='remove'>" +
83
"<input class='icon-remove' type='button' onclick='removeItemFromBasket("+id+")'>"+
84
"</td>"+
85
"</tr>"+
86
"</table>"+
87
"</li>"+
88
"<div class='well pull-right'>"+
89
"<input type='button' onclick='goToBasket()' class='btn btn-success' value='Tjek ud'>"+
90
"</div>"+
91
"</ul>"
92
93
)
94
}
95
updateTotal(amount,price);
96
updateBasketAmount();
97
}
98
});
99
}
100
Notifier.success('Vare tilføjet', 'Tilføjet'); // text and title are both optional.
101
}
102
function updateTotal(amount, price){
103
var price = parseFloat(price);
104
var oldValue = parseFloat($('#basket_total_cost').html());
105
var newPrice = amount*price+oldValue;
106
$('#basket_total_cost').html(newPrice);
107
}
108
function updateBasketAmount(){
109
var tmp = $('#basket_amount').html();
110
if(!isNaN(tmp)){
111
var oldAmount = parseInt(tmp.substr(0,2));
112
var i = oldAmount + 1;;
113
$('#basket_amount').html(
114
""+i+" vare(r)"
115
);
116
}else{
117
$('#basket_amount').html(
118
"1"+" vare(r)"
119
);
120
}
121
}
122
function goToBasket(){
123
window.location.href = myBaseUrl+'Products/basket';
124
}
125
Advertisement
Answer
I was having this problem when I started using select2 with XCrud. I solved it by disabling XCrud from loading JQuery, it was it a second time, and loading it below the body tag. So make sure JQuery isn’t getting loaded twice on your page.