Say I've got something like
<div id="left">
<div id="1"></div>
<div id="2"></div>
<div id="3"></div>
<div id="4"></div>
<div id="5"></div>
<div id="6"></div>
<div id="7"></div>
<div id="8"></div>
<div id="9"></div>
</div>
<div id="right">
<div id="r1"></div>
<div id="r2"></div>
<div id="r3"></div>
<div id="r4"></div>
<div id="r5"></div>
<div id="r6"></div>
<div id="r7"></div>
<div id="r8"></div>
<div id="r9"></div>
</div>
For the left
and right
div I've used css sprite to display an image.
For example, the left
div might look something like this
I want to be able to swap a child div from the left
with a child div from the right
and vice versa, or at least the image that divs contain. For example 1
and r1
I tried something like:
function swapElements(obj1, obj2) {
var parent2 = obj2.parentNode;
var next2 = obj2.nextSibling;
if (next2 === obj1) {
parent2.insertBefore(obj1, obj2);
} else {
obj1.parentNode.insertBefore(obj2, obj1);
if (next2) {
parent2.insertBefore(obj1, next2);
} else {
parent2.appendChild(obj1);
}
}
}
But it doesn't work swapping a div from left
with one from right
. Only ones that are within same div. For example 1
with 2
works, but r1
with 1
doesn't.
Hope I've made myself clear. Any help would be greatly appreciated.
Later edit; adding more code as requested:
Basically all div's within left
and right
have onclick="select((divs id))"
And the functions looks something like this
var selected = null;
function select (cellID){
if (selected == null){ //if this is the first time selecting
selected = cellID;
cell = document.getElementById(selected);
cell.setAttribute("class", "selected");
return;
}
if (selected == cellID){ // if selected same cell
document.getElementById(selected).setAttribute("class", "");
selected = null;
return;
}
if (selected){
document.getElementById(selected).setAttribute("class", "");
swapElements(document.getElementById(cellID), document.getElementById(selected));
selected = null;
return;
}
}
That selected
set attribute is for
.selected {
transform: scale(0.95, 0.95);
}