Note that there are some explanatory texts on larger screens.

plurals
  1. POdynamically changing CSS background-image
    primarykey
    data
    text
    <p>I'm fairly new to both PHP and Javascript, so please forgive my ignorance and poor use of terminology, but I'll do my best to explain exactly what I'm struggling to achieve.</p> <p>I have information stored in a PHP array that I call to my index page using the function below (the code below is in a separate PHP file called articles.php that's included in my index.php) : </p> <pre><code>&lt;?php function get_news_feed($article_id, $article) { $output = ""; $output .= '&lt;article class="img-wrapper"&gt;'; $output .= '&lt;a href="article.php?id=' . $article_id . '"&gt;'; $output .= '&lt;div class="news-heading"&gt;'; $output .= "&lt;h1&gt;"; $output .= $article["title"]; $output .= "&lt;/h1&gt;"; $output .= "&lt;p&gt;"; $output .= "Read more..."; $output .= "&lt;/p&gt;"; $output .= "&lt;/div&gt;"; $output .= '&lt;div id="news-img-1"&gt;'; $output .= "&lt;/div&gt;"; $output .= "&lt;/a&gt;"; $output .= "&lt;/article&gt;"; return $output; } $articles = array(); $articles[] = array( "title" =&gt; "Andy at NABA", "description" =&gt; "Docendi, est quot probo erroribus id.", "img" =&gt; "img/gym-01.jpg", "date" =&gt; "05/04/2013" ); $articles[] = array( "title" =&gt; "Grand Opening", "description" =&gt; "Docendi, est quot probo erroribus id.", "img" =&gt; "img/gym-01.jpg", "date" =&gt; "05/04/2013" ); ?&gt; </code></pre> <p>My index.php looks like the following minus some HTML that plays no role in this process:</p> <pre><code>&lt;?php include("inc/articles.php"); ?&gt; &lt;?php $pageTitle = "Home"; include("inc/header.php"); ?&gt; &lt;section class="col-4 news"&gt; &lt;?php $total_articles = count($articles); $position = 0; $news_feed = ""; foreach($articles as $article_id =&gt; $article) { $position = $position + 1; if ($total_articles - $position &lt; 2) { $news_feed .= get_news_feed($article_id, $article); } } echo $news_feed; ?&gt; &lt;/section&gt; </code></pre> <p>I am aiming to dynamically change the CSS Background-Image property of the div element with ID <strong>news-img-1</strong> using Javascript.</p> <p>I have tried such things as:</p> <pre><code>document.getElementById('news-img-1').style.backgroundImage = 'url('&lt;?php $article["img"]; ?&gt;')'; document.getElementById('news-img-1').style.backgroundImage = 'url('http://www.universalphysique.co.uk/' + '&lt;?php $article["img"]; ?&gt;')'; document.getElementById('news-img-1').style.backgroundImage = 'url('window.location.protocol + "//" + window.location.host + "/" + '&lt;?php $article["img"]; ?&gt;')'; </code></pre> <p>.....but I'm getting nowhere!! My code in practise works because the following Javascript inserts an image correctly:</p> <pre><code>document.getElementById('news-img-1').style.backgroundImage = 'url("img/gym-01.jpg")'; </code></pre> <p><a href="http://www.universalphysique.co.uk/" rel="nofollow">Here is my site up and running</a>, the images should be placed in the empty circles you'll see! Any help would be great, this ones tough for me!!</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