0

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

Photo

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);
        }
Doe Low
  • 1
  • 1
  • 2
  • pls add more code cause nobody knows when/how swapElements() gets called... – MarcelD Apr 10 '17 at 13:44
  • Sure, edited. Thanks – Doe Low Apr 10 '17 at 13:57
  • just checked in [jsbin](http://jsbin.com/jojoqajafu/edit?html,js,console,output) and it works as "expected" :) may your onclick does something weird? – MarcelD Apr 10 '17 at 14:02
  • @DoeLow - here is a [JSFiddle](https://jsfiddle.net/mpqcwm3h/1/) to help visualize. It seems to work. Beat by MarcelD by 43s. – Forty3 Apr 10 '17 at 14:04
  • Wow... I really don't know what to say. I've double checked everything and it all seems fine in my code yet it still won't swap from the left div to the right one. Could it be because I have images instead of text? I don't think that's the case either because it works if I try to swap from the same div (left or right) – Doe Low Apr 10 '17 at 14:08
  • it's maybe your onclick - inside the div's may you forgot to add quotation marks around your id ur passing to select() :) – MarcelD Apr 10 '17 at 14:17
  • Have a look at this answer of mine, it might help: [How to break an image into 9 small ones (sort of puzzle pieces)](http://stackoverflow.com/a/43322326/2827823) – Asons Apr 10 '17 at 14:19
  • [That's literally the first thing I've checked](http://imgur.com/a/CCR7i) I've tried quotation marks for only numeric parameters, tried without (which I'm positive is the correct way) and still no luck @ LGSon : I've actually tried that before, if I'm not mistaken I'm actually using your example here. – Doe Low Apr 10 '17 at 14:21

1 Answers1

0

Your approach using insertBefore is fundamentally the right approach.

Here's a working example, using insertBefore:

var leftDiv = document.getElementById('left');
var rightDiv = document.getElementById('right');
var divs = [... document.querySelectorAll('div div')];

function swapDivs() {
    var divIndex = (parseInt(this.id.substr(-1)) - 1);

    var leftBefore = rightDiv.getElementsByTagName('div')[divIndex];
    var leftAfter = leftDiv.getElementsByTagName('div')[(divIndex + 1)];

    leftDiv.insertBefore(leftBefore, leftAfter);

    var rightBefore = leftDiv.getElementsByTagName('div')[divIndex];
    var rightAfter = rightDiv.getElementsByTagName('div')[divIndex];

    rightDiv.insertBefore(rightBefore, rightAfter);
}

divs.forEach(function(div){
    div.addEventListener('click', swapDivs, false);
});
#left, #right {
float: left;
width: 180px;
background-color: rgb(127, 127, 127);
border: 2px solid rgb(0, 0, 0);
}

div div {
height: 24px;
text-align: center;
color: rgb(255, 255, 255);
background-color: rgb(255, 0, 0);
border: 2px solid rgb(255, 255, 255);
border-top-width: 1px;
border-bottom-width: 1px;
}

div div:hover {
background-color: rgb(163, 0, 0);
cursor: pointer;
}
<div id="left">
          <div id="l1">Left 1</div>
          <div id="l2">Left 2</div>
          <div id="l3">Left 3</div>
          <div id="l4">Left 4</div>
          <div id="l5">Left 5</div>
          <div id="l6">Left 6</div>
          <div id="l7">Left 7</div>
          <div id="l8">Left 8</div>
          <div id="l9">Left 9</div>
    </div>


    <div id="right">
          <div id="r1">Right 1</div>
          <div id="r2">Right 2</div>
          <div id="r3">Right 3</div>
          <div id="r4">Right 4</div>
          <div id="r5">Right 5</div>
          <div id="r6">Right 6</div>
          <div id="r7">Right 7</div>
          <div id="r8">Right 8</div>
          <div id="r9">Right 9</div>
    </div>
Rounin
  • 27,134
  • 9
  • 83
  • 108
  • I really think that something is off in my code and I don't know why... literally copy pasted the divs (so no more onclick) and the JS but it still won't work, damn. – Doe Low Apr 10 '17 at 14:37