Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:32:47+00:00 2026-06-06T00:32:47+00:00

I am trying to get it so that when a specific <li> is clicked,

  • 0

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):

enter image description here

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:32:48+00:00Added an answer on June 6, 2026 at 12:32 am

    There are many problems in your code.

    1. Do not use the onclick attribute. Especially do not use javascript to manually write the onclick attribute when you can just assign to the event directly.
    2. $('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.
    3. if the $ is jQuery you dont need to use $('photogal').innerHTML =... you can just do $('photogal').html("<img.../>")
    4. the last thing you mention is because you need a closure in your loop to evaluate the value of i immediately. Look up closures to understand what I mean.

    but I think your immediate problem is #2 above…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just trying to get the equivalent HTML code that represent a specific control
I am trying to get it so that when a link is clicked in
I'm trying to use prototype inheritance in Javascript. The problem with my code below
I am trying to pull posts from a specific month that has been clicked
So Here is the Problem, I am trying to get that circle to align
I'm trying to get it so that the alert message pops up if the
I'm just trying to get the numbers that are in a sentence. They can
I am trying to get a footer that will always stay at the bottom
i'm trying to get Color information that i've stored in a text file and
I am trying to get all users that are updated maximum 90 seconds ago:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.