Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I haven't run your code, but there's a few weird things, perhaps one of which may cause the issue or help clear up the code so the issue is more obvious.</p> <ul> <li>the variable <code>temp</code> seems to be an alias for <code>sb_button</code>: you could remove the variable declaration and replace all references with <code>temp</code></li> <li><code>sb_button</code> is a confusing name for an arbitrary child node</li> <li>you're appending the node in <code>sb_button</code> to <code>newparent</code> within the if statement, but right after you're trying to set <code>sb_button_.parentNode</code> to <code>parent</code> - that's <a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-1060184317" rel="nofollow">not possible since <code>parentNode</code> is readonly</a> and it certainly doesn't make sense - you can't append the element to one element but have a different parent.</li> <li>are you trying to copy or move nodes?</li> </ul> <p><strong>Edit:</strong> given that you want to copy nodes, I believe you're looking for <a href="https://developer.mozilla.org/En/DOM/Node.cloneNode" rel="nofollow"><code>cloneNode</code></a>: make a copy of the node and append that copy, not the original node.</p> <p>As a matter of clean design, when things get complicated, I'd avoid this kind of hard-to-reason-about while loop. Instead, simply make an array of the nodes, order those the way you want (you could even do this using <code>sort</code> to make it immediately obvious you're just rearranging things), and then make a function that takes a <code>newparent</code> and the array and appends copies of all elements in array order to <code>newparent</code>. Your example isn't that complex, but even here, I'd change the order of the if-clauses to have the "default" case in the final else. e.g.:</p> <pre><code>for(var child = parent.firstChild; child; child = child.nextSibling) if(child.id == curr_button.id) { //insert prev_button after curr_button newparent.appendChild(child.cloneNode(true)); newparent.appendChild(prev_button.cloneNode(true)); } else if(child.id != prev_button.id) { newparent.appendChild(child.cloneNode(true)); } parent.parentNode.replaceChild(newparent, parent); </code></pre> <p>The idea being to make it instantly obvious to the reader that all children are processed exactly once.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload