I rarely work with JS and when I do, I struggle to remember much! Anyhow, I’m trying to write a script that will reorder all the paragraphs inside an <article> element randomly. It’s easy enough to find scripts online to reorder elements of an array, but the array is usually just part of the script for demo purposes and the result is output to the console, so I need to build on them.
That’s all there will ever be on the page: an <article> and some short paragraphs (possibly up to 100 of them).
Rather than jumping into the code, I’m first trying to work out what steps need to be taken (to plan ahead and clarify what JS needs to do).
I was assuming the first step is to create an array of the paragraphs, but I’m starting to doubt that! If I do something like this:
let paraArray = getElementsByTagName("p");
I seem to get just a list of <p> tags without their content. Damn! Do I have to get the contents of the paragraphs separately? I was hoping that wouldn’t be necessary.
If so, I’m wondering if the script will need to do something like this:
create an array of the contents of each paragraph
reorder the array
place all those array items back in the DOM with <p> tags wrapped around them.
Can anyone point me in the right direction for how to approach this?
When you use getElementsByTagName you get a NodeList of paragraph elements, including their contents. You don’t need to separately fetch the text inside each paragraph.
Here’s what I would do:
Select all the paragraphs
Convert the NodeList to Array using Array.from().
Shuffle the array using a shuffling algorithm you can find online
Clear the current content of the <article> andre- append the shuffled paragraphs back to it.
Thanks very much, Jim. I’ll have to look into 2. and 4., but that’s a good start!
Haha, no, all answers welcome! To be honest, I prefer to look through finished code and try to figure out how it works than figure it out myself. But it felt rude to just ask for a solution, and it’s worth trying to figure it out as well. Thanks for your solution, though. I appreciate it.
Ah, very clever. Nice work. I even almost can understand it.
The CSS order property allows you to change the display order of elements within a CSS Flexbox container or a CSS Grid container. That’s why I changed the CSS of <article> to display: flex.
Fortunately the integer values of the order properties do not need to be sequential 1, 2, 3, 4, 5 and 6.
parseInt(Math.random()*1e9) generates random integers between 0 and 1,000,000,000. I chose a fairly large exponent 9 to tend to minimise the chance of generating two or more identical integers. Actually it does not matter if identical integers are generated: the corresponding paragraphs would be shown in original order. Strictly therefore the paragraphs will not be absolutely perfectly in random order.
The forEach loops through the <p> elements and uses an arrow function to set the CSS order value of each element to a random integer (with implicit type conversion to string). I chose the variable e to represent each element. A traditional for loop could have been used.
Screenreaders will probably read out the paragraphs in original order.
Yes, and in Grid too. As it turns out, I am using Grid for the layout of these pages, but that’s an aside.
thanks for the explanation. It was more the JS bit I was getting my head around. I vaguely remember that exponent syntax, but wouldn’t have come up with it myself.
I did wonder about that. @james_hibbard — Is there a particular reason you suggested clearing the content explicitly, or a best way to do it explicitly?