Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, appending to <code>innerHTML</code> is usually very bad. If you run the code like <code>something.innerHTML += 'lots of html'</code> in a loop, browser has to update the DOM tree and re-render the page on every iteration. This may greatly slow down the things. <strong>Use the buffer string instead</strong>.</p> <p>I also suspect that this might have caused issues with event attachment. Try attaching events after all the <code>div</code>s have been attached to the tree. The code in this case would be something like this:</p> <pre><code>function newsstories() { // Use a variable that would store the newly generated HTML: var html = "&lt;h1 class='newsheader'&gt;Latest News&lt;/h1&gt;"; xmlhttp=new XMLHttpRequest(); xmlhttp.open("POST","test.com?uri=loadnews",false); xmlhttp.send(); var newsreponse = JSON.parse(xmlhttp.responseText); for (var i = 0, len = newsreponse.length; i &lt; len; ++i) { var news = newsreponse[i]; // Notice the 'var' keyword! if(i % 2 == 0){ var cssclass = "even"; } else { var cssclass = "odd"; } // Append to our buffer variable html += "&lt;div class='news " + cssclass + "'&gt;&lt;div class='newstitle'&gt;&lt;div class='newstitlecolor'&gt;&lt;a href='#' onclick='loadnewsstory();'&gt;" + news.post_title + "&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class='base' style='background: url('" + news.featured_image + "');'&gt;&lt;img src='" + news.featured_image + "' style='width:100%; height:100%;' id='news_"+ i +"'/&gt;&lt;/div&gt;&lt;/div&gt;"; } // Now that new html is ready, insert it into the tree // and attach events document.getElementById("activecontent").innerHTML = html; for (var i = 0, len = newsreponse.length; i &lt; len; ++i) { var img = document.getElementById('news_'+i); if(img) img.addEventListener('click', loadnewsstory); } } </code></pre>
    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. 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