Why does this simple jQuery not work?
http://page-test.co.uk/jq.html
It should result in this:
<div class="main">
</div>
<div>1</div>
<div>2</div>
<div>3</div>
| SitePoint Sponsor |


Why does this simple jQuery not work?
http://page-test.co.uk/jq.html
It should result in this:
<div class="main">
</div>
<div>1</div>
<div>2</div>
<div>3</div>

Hi,
The problem is caused by the string variable stored_html.
You can do what you want by making it an array.
Code JavaScript:var stored_html = new Array(); $(".main > div").each(function() { stored_html.push($(this)); }); $(stored_html.reverse()).each(function(){ $(this).insertAfter($(this).parent()); });
Obviously, this is not the best way to accomplish the small task you describe above, but as I'm sure that there's a lot more going on in your script that this, I tried to keep your code as it was.
I would be grateful if anyone else reading this can give a clearer insight as to what exactly was not working in the original code.
Also, I would be glad to find out if there is a better way of doing this (I'm sure there is)
Bookmarks