Hi! I’m kind of new to JS, so I’ll try explain my problem as clear as I can. Suppose I have this simplified function:
var counter = 0;
var labels = new Array();
function abc(text){
var labelIndex = counter;
var mainContainer = document.getElementById('mainContainer');
var labelContainer = document.createElement('div');
var label= document.createTextNode(text);
container.appendChild(label);
mainContainer.appendChild(labelContainer);
labels[counter] = text;
}
It’s clear that I’m just creating some divs with text inside them and showing them on screen in a pre-created div (mainContainer). What I want to do is to save each element (text), when the user clicks, for example, a button ‘Save’. That is not a problem, if I write these values (inside my function) to a global array. So in the end I will have something like abcArray[0]=‘1st element’; abcArray[1]=‘2nd element’ - everything’s ok for now.
Now I want to add a function inside my abc function, which allows the user to swap (with places) 2 divs he created:
downButton.onclick = function() {
pushDown(labelContainer);
}
function pushDown(div) {
var next;
next = findNext(div);
if (div) {
div.parentNode.insertBefore(next, div);
}
}
On screen everything seems cool, but what about my array - how do I do this? The problem that I’m having is receiving the correct array index to swap the 2 elements. If I use labelIndex variable, I’ll be able to swap the divs just for one time, from that moment everything is a mess, because from downButton.onclick function I can’t access the labelIndex variable to change it .
Any help appreciated.