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 3235436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:30:22+00:00 2026-05-17T17:30:22+00:00

I’ve a ul li menu such like: <ul> <li><a href=#1>Image #1</a></li> <li><a href=#2>Image #2</a></li>

  • 0

I’ve a ul li menu such like:

<ul>
     <li><a href="#1">Image #1</a></li>
     <li><a href="#2">Image #2</a></li>
     <li><a href="#3">Image #3</a></li>
     <li><a href="#4">Image #4</a></li>
</ul>

And I have a container with an image like:

<div id="image-container">
     <img src="images/1.jpg">
</div>

I want to switch the image in “image-container” div using “hash” and “load” (every image is in /image/ folder and each is named like hashes, so loads images/2.jpg etc.

I’m trying this way:

$("#ul li:nth-child(1)").click(function(e) { 
        e.preventDefault();   
        var hash = this.parentNode.hash;  
        $("#image-container").load('images/'+ hash.substring(1) +'.jpg');

$("#ul li:nth-child(2)").click(function(e) { 
        e.preventDefault();   
        var hash = this.parentNode.hash;  
        $("#image-container").load('images/'+ hash.substring(1) +'.jpg');

$("#ul li:nth-child(3)").click(function(e) { 
        e.preventDefault();   
        var hash = this.parentNode.hash;  
        $("#image-container").load('images/'+ hash.substring(1) +'.jpg');

(...)

But it doesn’t work so well (I guess I’m going to need something slightly different than load, because it won’t change my image anyways?).

Thanks a lot.

And by the way – is there a way to write my jQuery code shorter (in one function, because now I have multiple with the same body)?

Thanks a lot,
Ada 🙂

  • 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-05-17T17:30:23+00:00Added an answer on May 17, 2026 at 5:30 pm

    You can write your code a bit shorter like this:

    $("ul li a").click(function(e) { 
      e.preventDefault();   
      $("#image-container img").attr('src', 'images/'+ this.hash.substring(1) +'.jpg');
    });
    

    You can try it out here, a few changes from your original:

    • Attach the handler to the <a>, it has the hash
    • You can bind the same handler to many elements, just use a selector that grabs all the element you want
    • Get the has from this since this refers to the <a> element
    • You <ul> doesn’t have a id="ul" attribute, so the selector should be ul, not #ul
    • Don’t use .load() which is for fetching content, instead set the src if the <img> using .attr().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.