Note that there are some explanatory texts on larger screens.

plurals
  1. POusing javascript shuffled integers to populate sequential html areas
    primarykey
    data
    text
    <p>I'm trying to change the contents of sequential divs with randomly-ordered contents. It's not working so well.</p> <p>I have about 25 <code>&lt;div&gt;</code> tags that, because of other javascripts regulating touch behaviour, are unable to be put in a randomised order using the usual methods. So instead, I'm trying to have the <em>contents</em> of those <code>&lt;div&gt;</code>s randomised by setting different <code>innerHTML</code> values when the document is loaded.</p> <p>I have a script that provides random integers as I need, but then I'm having trouble getting it to populate the ordered divs. </p> <p>Here's what I'm using that isn't working:</p> <pre><code>ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(25); for(int i = 0; i &lt; 25; i++) { list.add(i); } Collections.shuffle(list); Object[] randomNumbers = (Object[])list.toArray(); function testit() { for(int i = 0; i &lt; 25; i++) { var a = document.getElementById('if'+i); a.innerHTML = randomNumbers[i]; } } </code></pre> <p>What I'd like to see is that this takes all my <code>&lt;div&gt;</code>s which have id's such as "if1" "if2" and so one and then set the content of that <code>&lt;div&gt;</code> with the randomised numbers. </p> <p>I know <code>document.getElementById('if'+i)</code> is surely the wrong way to get ID "if1" then "if2", but I'm not really sure how else it can be done.</p> <p>Basically I just need a way to populate 25 absolutely-positions <code>&lt;div&gt;</code>s with 25 chunks of HTML content but in a random order. I'd think that could be done by pairing sequential numbers to randomly ordered numbers and tying those into <code>&lt;div&gt;</code> id's. Normally I'd just do this with PHP and be done already but I need it to work as an offline mobile webapp so I'm trying to do it with JS and CSS. I'm definitely open to other approaches.</p> <p><strong>edit:</strong> If the code above is Java and not Javascript, that's the problem. Then I guess I'm looking for alternative ways, in Javascript, to produce random, non-repeating integers and link those to sequential integers, all between 1 and 25</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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