I've been sticking around with a problem for a while and can't find any solutions. Even though I've read this question and answers on stackoverflow, I can't figure out whats wrong. Probably due to a lack of good english. So German answers are welcome.
However, I'm trying to pass a variable into another function outside the for-loop. Here's my code:
"use strict";
// gobal vars
var sort = new Array();
$(document).ready(function(){
// Sortable.js
// Copyright 2013-2015 Lebedev Konstantin <ibnRubaXa@gmail.com>
// http://rubaxa.github.io/Sortable/
var min = 2,
max = 5,
elems = new Array();
elems[0] = document.getElementById("left");
elems[1] = document.getElementById("center");
elems[2] = document.getElementById("right");
for(var i=0, len=elems.length; i<len; i++) {
var amnt = elems[i].getElementsByTagName("div").length,
group = "gewicht";
if (amnt <= min) {
group = {name: "gewicht", pull: false, put: true};
} else if (amnt >= max) {
group = {name: "gewicht", pull: true, put: false};
}
sort[i] = Sortable.create(elems[i], {
group: group,
animation: 150,
ghostClass:"ghost",
draggable: ".draggable",
onSort: function(evt) {sortEnd(evt, i, min, max)}
})
}
});
function sortEnd(evt, j, min, max) {
var targ = evt.target,
amnt = targ.getElementsByTagName("div").length;
console.log(j);
amnt = targ.getElementsByTagName("div").length;
if (amnt <= min) {
sort[j].option("group", {name: "gewicht", pull: false, put: true});
} else if (amnt > max) {
sort[j].option("group", {name: "gewicht", pull: true, put: false});
} else {
sort[j].option("group", {name: "gewicht", pull: true, put: true});
}
}
The problem is at onSort: function(evt) {sortEnd(evt, i, min, max)}
, where the variable i inside the for loop will be 0, 1 and 2. But outside the loop, the console.log()
in the function sortEnd()
it will return 3.
An explanation which I can understand would be good, a solution for my problem even better, both would be best.
Thanks