I am trying to get it so that when a specific <li> is clicked, an image is displayed on the same page.
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.
<?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 > 20)?20:$c;
for ($i = 0; $i < $c; $i++) {
$lines[$i] = trim($lines[$i]);
if (file_exists($lines[$i])) {
echo "<li id=\"item$j\" title=\"".$lines[$i]."\" onclick=\"\"><a id=\"link$j\" href=\"#\">".$lines[$i]."</a></li>";
$j++;
}
}
}
?>
The following javascript applies the onclick to each element:
<script type="text/javascript">
for (i = 0; i < <?php echo $j; ?>; i++) {
iLINK = $("item"+i).get("title");
iHTML = "<img src=\""+iLINK+"\" />";
$("item"+i).setAttribute("onclick","$('photogal').innerHTML = " +iHTML);
}
</script>
When loading the page, the <li> elements are aesthetically fine. When inspecting the element I see the following (which appears to be correct):

When changing the last line of javascript to:
$("item"+i).setAttribute("onclick","$('photogal').innerHTML = iHTML");
the onclick 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).
What is the problem in this code?
There are many problems in your code.
onclickattribute. Especially do not use javascript to manually write the onclick attribute when you can just assign to the event directly.$('photogal').innerHTML = <img...../>is wrong because it is incorrect JS syntax. Try wtiting that line directly into your JS and it will complain about<img.../>not being wrapped in quotes.$is jQuery you dont need to use$('photogal').innerHTML =...you can just do$('photogal').html("<img.../>")iimmediately. Look up closures to understand what I mean.but I think your immediate problem is #2 above…