Note that there are some explanatory texts on larger screens.

plurals
  1. POonClick events not firinng with innerHTML
    primarykey
    data
    text
    <p>Hi I have an issue with onclick events being writen using innerHTML I have tried doing it two ways and both have not work</p> <p>the function it needs to call is </p> <pre><code>function loadnewsstory() { alert("123456"); } </code></pre> <p>at the moment it should show the alert box with 123456 but it is not firing.</p> <p>the function loading it is the following</p> <pre><code>function newsstories() { document.getElementById("activecontent").innerHTML = "&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); var countstories = 0; for (var i = 0, len = newsreponse.length; i &lt; len; ++i) { var news = newsreponse[i]; if(i % 2 == 0){ cssclass = "even"; } else { cssclass = "odd"; } // alert(news.featured_image); document.getElementById("activecontent").innerHTML += "&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_"+ countstories +"'/&gt;&lt;/div&gt;&lt;/div&gt;"; document.getElementById("news_"+countstories).onclick = function(){ loadnewsstory();} countstories++; } } </code></pre> <p>as you can see I have also ran <code>document.getElementById("news_"+countstories).onclick = function(){ loadnewsstory();}</code> because i read that onclick events could not be written by javascript innerHTML which I know I have been able to do before. If someone else knows of a fix to this issue that would be great. this is for a cordova iphone app</p> <p>Thanks</p> <p><strong>EDIT</strong></p> <p>I have found that this</p> <pre><code>document.getElementById("activecontent").innerHTML += "&lt;div class='news " + cssclass + "'&gt;&lt;a href='javascript:openextlink();'&gt;&lt;img src='" + news.featured_image + "' style='width:100%; height:100%;'/&gt;&lt;/a&gt;&lt;/div&gt;"; </code></pre> <p>works but it seems to only work on the last news story, am I missing something.</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.
 

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