Reverse order of divs in pure plain JavaScript no CSS
up vote
1
down vote
favorite
I can't find a post on here that is not jQuery based I need a plain JavaScript example and please don't suggest anything CSS solution related. I am aware that there is CSS methods to do things similar to this but for personal reasons I need a JavaScript solution for this instead.
Ok I need to make all the .x class names div's to be in reverse order so it will look like this
D
C
B
A
At default it looks like this
A
B
C
D
and I want the scroll box of the scroll bar on page load to start at the bottom with the reverse order of the .x div's like this photo shop image I created.
Here is my code currently
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
javascript
add a comment |
up vote
1
down vote
favorite
I can't find a post on here that is not jQuery based I need a plain JavaScript example and please don't suggest anything CSS solution related. I am aware that there is CSS methods to do things similar to this but for personal reasons I need a JavaScript solution for this instead.
Ok I need to make all the .x class names div's to be in reverse order so it will look like this
D
C
B
A
At default it looks like this
A
B
C
D
and I want the scroll box of the scroll bar on page load to start at the bottom with the reverse order of the .x div's like this photo shop image I created.
Here is my code currently
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
javascript
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
Check my answer to your question.
– hev1
Nov 8 at 22:46
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can't find a post on here that is not jQuery based I need a plain JavaScript example and please don't suggest anything CSS solution related. I am aware that there is CSS methods to do things similar to this but for personal reasons I need a JavaScript solution for this instead.
Ok I need to make all the .x class names div's to be in reverse order so it will look like this
D
C
B
A
At default it looks like this
A
B
C
D
and I want the scroll box of the scroll bar on page load to start at the bottom with the reverse order of the .x div's like this photo shop image I created.
Here is my code currently
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
javascript
I can't find a post on here that is not jQuery based I need a plain JavaScript example and please don't suggest anything CSS solution related. I am aware that there is CSS methods to do things similar to this but for personal reasons I need a JavaScript solution for this instead.
Ok I need to make all the .x class names div's to be in reverse order so it will look like this
D
C
B
A
At default it looks like this
A
B
C
D
and I want the scroll box of the scroll bar on page load to start at the bottom with the reverse order of the .x div's like this photo shop image I created.
Here is my code currently
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
/*???*/
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
javascript
javascript
asked Nov 7 at 9:03
fsofb
294110
294110
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
Check my answer to your question.
– hev1
Nov 8 at 22:46
add a comment |
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
Check my answer to your question.
– hev1
Nov 8 at 22:46
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
Check my answer to your question.
– hev1
Nov 8 at 22:46
Check my answer to your question.
– hev1
Nov 8 at 22:46
add a comment |
4 Answers
4
active
oldest
votes
up vote
0
down vote
Pure javascript solution to reverse the divs
var divElems = document.getElementsByClassName('x');
var divB = document.getElementById('b');
var output = ;
for(var i =divElems.length - 1; i >= 0;i--)
output.push(divElems[i]);
b.innerHTML = '';
output.forEach(function(eachDiv)
b.appendChild(eachDiv);
);
Working demo - https://jsfiddle.net/71kpLqne/
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
add a comment |
up vote
0
down vote
You can use document.querySelectorAll
to get all of the elements with a class of x
and you can convert it to an Array with .slice.call
. You can then set the #b
element's innerHTML to ''
which removes all elements inside it and reverse the Array of elements with a class x
and use forEach
to append each element to the #b
element.
You can use elem.scrollTop = elem.scrollHeight;
to scroll elem
to the bottom.
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
JSFiddle Demo: http://jsfiddle.net/nfwcp960/embedded/result
add a comment |
up vote
0
down vote
I have provided a link in the comments and here to a previous similar question with a number of valid answers:
Previous Question
For your question specifically, you can add the following pure JavaScript :
<script>
window.onload = function()
function reverseEls(elem)
elem = document.getElementById(elem);
for (var i=0;i<elem.childNodes.length;i++)
elem.insertBefore(elem.childNodes[i], elem.firstChild);
</script>
This will reverse your divs as desired and also scroll to the bottom of the page.
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
add a comment |
up vote
0
down vote
You can firstly get all the divs using document.querySelectorAll('.x');
Then reverse this order using .reverse()
. Then you can turn this array of DOM elements into a string of 'string' DOM elements using .reduce()
:
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
You can even simplify this js into a one-liner if you wish:
document.querySelector('#b').innerHTML = [...document.querySelectorAll('.x')].reverse().reduce((a, e) => a+e.outerHTML, '');
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Pure javascript solution to reverse the divs
var divElems = document.getElementsByClassName('x');
var divB = document.getElementById('b');
var output = ;
for(var i =divElems.length - 1; i >= 0;i--)
output.push(divElems[i]);
b.innerHTML = '';
output.forEach(function(eachDiv)
b.appendChild(eachDiv);
);
Working demo - https://jsfiddle.net/71kpLqne/
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
add a comment |
up vote
0
down vote
Pure javascript solution to reverse the divs
var divElems = document.getElementsByClassName('x');
var divB = document.getElementById('b');
var output = ;
for(var i =divElems.length - 1; i >= 0;i--)
output.push(divElems[i]);
b.innerHTML = '';
output.forEach(function(eachDiv)
b.appendChild(eachDiv);
);
Working demo - https://jsfiddle.net/71kpLqne/
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
add a comment |
up vote
0
down vote
up vote
0
down vote
Pure javascript solution to reverse the divs
var divElems = document.getElementsByClassName('x');
var divB = document.getElementById('b');
var output = ;
for(var i =divElems.length - 1; i >= 0;i--)
output.push(divElems[i]);
b.innerHTML = '';
output.forEach(function(eachDiv)
b.appendChild(eachDiv);
);
Working demo - https://jsfiddle.net/71kpLqne/
Pure javascript solution to reverse the divs
var divElems = document.getElementsByClassName('x');
var divB = document.getElementById('b');
var output = ;
for(var i =divElems.length - 1; i >= 0;i--)
output.push(divElems[i]);
b.innerHTML = '';
output.forEach(function(eachDiv)
b.appendChild(eachDiv);
);
Working demo - https://jsfiddle.net/71kpLqne/
answered Nov 7 at 9:19
front_end_dev
1,3151511
1,3151511
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
add a comment |
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
Thanks front_end_dev for your reply the reverse ordering works great but how can I set the scroll box at the bottom on page load?
– fsofb
Nov 7 at 19:52
add a comment |
up vote
0
down vote
You can use document.querySelectorAll
to get all of the elements with a class of x
and you can convert it to an Array with .slice.call
. You can then set the #b
element's innerHTML to ''
which removes all elements inside it and reverse the Array of elements with a class x
and use forEach
to append each element to the #b
element.
You can use elem.scrollTop = elem.scrollHeight;
to scroll elem
to the bottom.
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
JSFiddle Demo: http://jsfiddle.net/nfwcp960/embedded/result
add a comment |
up vote
0
down vote
You can use document.querySelectorAll
to get all of the elements with a class of x
and you can convert it to an Array with .slice.call
. You can then set the #b
element's innerHTML to ''
which removes all elements inside it and reverse the Array of elements with a class x
and use forEach
to append each element to the #b
element.
You can use elem.scrollTop = elem.scrollHeight;
to scroll elem
to the bottom.
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
JSFiddle Demo: http://jsfiddle.net/nfwcp960/embedded/result
add a comment |
up vote
0
down vote
up vote
0
down vote
You can use document.querySelectorAll
to get all of the elements with a class of x
and you can convert it to an Array with .slice.call
. You can then set the #b
element's innerHTML to ''
which removes all elements inside it and reverse the Array of elements with a class x
and use forEach
to append each element to the #b
element.
You can use elem.scrollTop = elem.scrollHeight;
to scroll elem
to the bottom.
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
JSFiddle Demo: http://jsfiddle.net/nfwcp960/embedded/result
You can use document.querySelectorAll
to get all of the elements with a class of x
and you can convert it to an Array with .slice.call
. You can then set the #b
element's innerHTML to ''
which removes all elements inside it and reverse the Array of elements with a class x
and use forEach
to append each element to the #b
element.
You can use elem.scrollTop = elem.scrollHeight;
to scroll elem
to the bottom.
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
JSFiddle Demo: http://jsfiddle.net/nfwcp960/embedded/result
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
<script>
var container = document.querySelector('#b');
var elems = .slice.call(document.querySelectorAll('.x'));
container.innerHTML = "";
elems.reverse().forEach(function(elem, index)
container.appendChild(elem);
);
container.scrollTop = container.scrollHeight;
//scroll to bottom of #b div
var body = document.body,
html = document.documentElement;
var docheight = Math.max( body.scrollHeight, body.offsetHeight,
html.clientHeight, html.scrollHeight, html.offsetHeight );
//get height of document cross-browser
window.scrollTo(0, docheight);
//scroll to bottom of document
</script>
edited Nov 8 at 2:28
answered Nov 8 at 2:22
hev1
5,4653527
5,4653527
add a comment |
add a comment |
up vote
0
down vote
I have provided a link in the comments and here to a previous similar question with a number of valid answers:
Previous Question
For your question specifically, you can add the following pure JavaScript :
<script>
window.onload = function()
function reverseEls(elem)
elem = document.getElementById(elem);
for (var i=0;i<elem.childNodes.length;i++)
elem.insertBefore(elem.childNodes[i], elem.firstChild);
</script>
This will reverse your divs as desired and also scroll to the bottom of the page.
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
add a comment |
up vote
0
down vote
I have provided a link in the comments and here to a previous similar question with a number of valid answers:
Previous Question
For your question specifically, you can add the following pure JavaScript :
<script>
window.onload = function()
function reverseEls(elem)
elem = document.getElementById(elem);
for (var i=0;i<elem.childNodes.length;i++)
elem.insertBefore(elem.childNodes[i], elem.firstChild);
</script>
This will reverse your divs as desired and also scroll to the bottom of the page.
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
add a comment |
up vote
0
down vote
up vote
0
down vote
I have provided a link in the comments and here to a previous similar question with a number of valid answers:
Previous Question
For your question specifically, you can add the following pure JavaScript :
<script>
window.onload = function()
function reverseEls(elem)
elem = document.getElementById(elem);
for (var i=0;i<elem.childNodes.length;i++)
elem.insertBefore(elem.childNodes[i], elem.firstChild);
</script>
This will reverse your divs as desired and also scroll to the bottom of the page.
I have provided a link in the comments and here to a previous similar question with a number of valid answers:
Previous Question
For your question specifically, you can add the following pure JavaScript :
<script>
window.onload = function()
function reverseEls(elem)
elem = document.getElementById(elem);
for (var i=0;i<elem.childNodes.length;i++)
elem.insertBefore(elem.childNodes[i], elem.firstChild);
</script>
This will reverse your divs as desired and also scroll to the bottom of the page.
edited Nov 8 at 9:42
answered Nov 7 at 9:24
cmprogram
1,053519
1,053519
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
add a comment |
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
Thanks cmprogram the reverse ordering works great but sadly the scroll box does not set at the bottom on page load any ideas why is that?
– fsofb
Nov 7 at 19:48
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
@fsofb Apparently the previous scroll does not work in ALL browsers. I have amended my answer.
– cmprogram
Nov 8 at 9:42
add a comment |
up vote
0
down vote
You can firstly get all the divs using document.querySelectorAll('.x');
Then reverse this order using .reverse()
. Then you can turn this array of DOM elements into a string of 'string' DOM elements using .reduce()
:
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
You can even simplify this js into a one-liner if you wish:
document.querySelector('#b').innerHTML = [...document.querySelectorAll('.x')].reverse().reduce((a, e) => a+e.outerHTML, '');
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
add a comment |
up vote
0
down vote
You can firstly get all the divs using document.querySelectorAll('.x');
Then reverse this order using .reverse()
. Then you can turn this array of DOM elements into a string of 'string' DOM elements using .reduce()
:
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
You can even simplify this js into a one-liner if you wish:
document.querySelector('#b').innerHTML = [...document.querySelectorAll('.x')].reverse().reduce((a, e) => a+e.outerHTML, '');
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
add a comment |
up vote
0
down vote
up vote
0
down vote
You can firstly get all the divs using document.querySelectorAll('.x');
Then reverse this order using .reverse()
. Then you can turn this array of DOM elements into a string of 'string' DOM elements using .reduce()
:
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
You can even simplify this js into a one-liner if you wish:
document.querySelector('#b').innerHTML = [...document.querySelectorAll('.x')].reverse().reduce((a, e) => a+e.outerHTML, '');
You can firstly get all the divs using document.querySelectorAll('.x');
Then reverse this order using .reverse()
. Then you can turn this array of DOM elements into a string of 'string' DOM elements using .reduce()
:
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
You can even simplify this js into a one-liner if you wish:
document.querySelector('#b').innerHTML = [...document.querySelectorAll('.x')].reverse().reduce((a, e) => a+e.outerHTML, '');
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
const divs = [...document.querySelectorAll('.x')];
const new_order = divs.reverse().reduce((acc, elem) => acc+elem.outerHTML, '');
document.querySelector('#b').innerHTML = new_order;
#a
background-color: gold;
height: 500px;
width: 500px;
border-radius: 8px;
position: relative;
color: red;
#b
background-color: orange;
height: 90%;
width: 90%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
overflow-y: auto;
overflow-x: hidden;
.x
background-color: blue;
display: block;
height: 200px;
width: 100%;
position: relative;
border: 2px solid white;
<div id='a'>
<div id='b'>
<div class='x'>
<h1>A</h1>
</div><!--</x>-->
<div class='x'>
<h1>B</h1>
</div><!--</x>-->
<div class='x'>
<h1>C</h1>
</div><!--</x>-->
<div class='x'>
<h1>D</h1>
</div><!--</x>-->
</div><!--</b>-->
</div><!--</a>-->
edited Nov 20 at 10:10
answered Nov 7 at 9:19
Nick Parsons
3,0952620
3,0952620
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
add a comment |
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
Thanks Nick Parsons for your reply the reverse ordering works great but how can I set the scroll box to be set at the bottom on page load?
– fsofb
Nov 7 at 19:51
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
@fsofb so you wan't the scroll box to be at the bottom of the page? This is difficult to do without css... is adding css via javascript an option?
– Nick Parsons
Nov 8 at 1:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186278%2freverse-order-of-divs-in-pure-plain-javascript-no-css%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
This question has been asked here and has a solution based in plain Javascript that should work for you by user Ayman Abdel-Rahman.
– cmprogram
Nov 7 at 9:08
Thanks for your reply I saw that post earlier and I could not get it to work with my code if you know how you can get this to work with my code can you give an example by that post if you know how to get it to work if you don't know how that's ok I failed at getting that method to work with my code.
– fsofb
Nov 7 at 9:14
I have provided an answer to your specific question.
– cmprogram
Nov 7 at 9:25
Check my answer to your question.
– hev1
Nov 8 at 22:46