Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I know what you mean, I may be wrong.</p> <p>You want to select an image from an array containing strings of HTML which also happens to contain an image tag, you want to use the <code>src</code> attribute value for the background image of your table.</p> <p>You would use a regular expression to do that in a for loop or join the array to a string first, could be used, but as Adam Botley mentioned, store each URL in the array, it will make your life a whole lot easier.</p> <p>It looks like your code is from a tutorial, so if all you really want to do is randomly select a background image for the table that's even easier.</p> <p>Here is what you can do. Create an array of URLs. </p> <pre><code>var urls = ['http://lorempixel.com/200/200', 'http://lorempixel.com/200/200']; </code></pre> <p>Then you need to calculate a random number to use for the key. </p> <pre><code>var index = Math.floor(Math.random() * urls.length); </code></pre> <p>A <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random" rel="nofollow">random number in Javascript</a> is floating point number between 0 and 1, so you multiply this by the amount of URLs in the array. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor" rel="nofollow">Math.floor()</a> returns the integer of the argument.</p> <p>Then you'll need a reference to the table element. </p> <pre><code>var theTableElement = document.getElementById('theTableElement'); </code></pre> <p>For that to actually work you must add an id to the table in the HTML. </p> <pre><code>&lt;table id="theTableElement"&gt; &lt;tr&gt; &lt;td&gt;hello&lt;/td&gt; &lt;td&gt;there&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;you&lt;/td&gt; &lt;td&gt;person :)&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Now, to set the background image, you can just. </p> <pre><code>theTableElement.style.background = 'url(' + urls[index] + ')'; </code></pre> <p>Now It should work, let me know!</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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