Note that there are some explanatory texts on larger screens.

plurals
  1. POonclick not working on <li>
    primarykey
    data
    text
    <p>I am trying to get it so that when a specific <code>&lt;li&gt;</code> is clicked, an image is displayed on the same page. </p> <p>Below is the code that determines which images to use. It seems to work fine, but I am including it anyway in case it contributes to the problem.</p> <pre><code> &lt;?php $j = 0; if (file_exists("u/".$_SERVER["REMOTE_ADDR"])) { $lines = array_reverse(array_unique(file("u/".$_SERVER["REMOTE_ADDR"]))); $c = count($lines); $c = ($c &gt; 20)?20:$c; for ($i = 0; $i &lt; $c; $i++) { $lines[$i] = trim($lines[$i]); if (file_exists($lines[$i])) { echo "&lt;li id=\"item$j\" title=\"".$lines[$i]."\" onclick=\"\"&gt;&lt;a id=\"link$j\" href=\"#\"&gt;".$lines[$i]."&lt;/a&gt;&lt;/li&gt;"; $j++; } } } ?&gt; </code></pre> <p>The following javascript applies the onclick to each element:</p> <pre><code> &lt;script type="text/javascript"&gt; for (i = 0; i &lt; &lt;?php echo $j; ?&gt;; i++) { iLINK = $("item"+i).get("title"); iHTML = "&lt;img src=\""+iLINK+"\" /&gt;"; $("item"+i).setAttribute("onclick","$('photogal').innerHTML = " +iHTML); } &lt;/script&gt; </code></pre> <p>When loading the page, the <code>&lt;li&gt;</code> elements are aesthetically fine. When inspecting the element I see the following (which appears to be correct):</p> <p><img src="https://i.stack.imgur.com/Or8On.png" alt="enter image description here"></p> <p>When changing the last line of javascript to:</p> <pre><code>$("item"+i).setAttribute("onclick","$('photogal').innerHTML = iHTML"); </code></pre> <p>the <code>onclick</code> works as expected, although all links cause the final image to show (because after the loop, iHTML ends up being whatever the last instance of iHTML was).</p> <p>What is the problem in this code?</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.
 

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