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

  • Home
  • SEARCH
  • 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 7416943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:36:03+00:00 2026-05-29T07:36:03+00:00

So I’m very beginner with javascript and would love some help simplifying this code.

  • 0

So I’m very beginner with javascript and would love some help simplifying this code.

I have a series of thumbnails arranged in a specific pattern, and when you click on a thumbnail, I’d like all the thumbnails to disappear, and the corresponding larger image to become visible. Then, when you click on the large image, it disappears and all the thumbnails are visible again. Each thumbnail has its own div id because they all have their unique positions.
I’ve figured out a way to do it, but it’s very repetitive.

HTML:

<style type="text/css"> 
  #largeimage_wrapper {visibility: hidden;}
</style> 

</head>

<body>
<div id="thumbnail_wrapper">
    <div id="thumbnail1"><img src="thumbnail1.jpg" onClick="get_big1();"/></div>
    <div id="thumbnail2"><img src="thumbnail2.jpg" onClick="get_big2();"/></div>
    <div id="thumbnail3"><img src="thumbnail3.jpg" onClick="get_big3();"/></div>  
    ...etc
</div>


<div id="largeimage_wrapper">
    <div id="large1"><img src="thumbnail1.jpg" onClick="get_thumbs1();"/></div>
    <div id="large2"><img src="thumbnail2.jpg" onClick="get_thumbs2();"/></div>
    <div id="large3"><img src="thumbnail3.jpg" onClick="get_thumbs3();"/></div>
    ...etc
</div>

</body>

javascript:

get_big1() {
document.getElementById('thumbnailwrapper').style.visibility='hidden';
document.getElementById('large1').style.visibility='visible';
}
get_thumbs1() {
document.getElementById('thumbnailwrapper').style.visibility='visible';
document.getElementById('large1').style.visibility='hidden';
}

get_big2() {
document.getElementById('thumbnailwrapper').style.visibility='hidden';
document.getElementById('large2').style.visibility='visible';
}
get_thumbs2() {
document.getElementById('thumbnailwrapper').style.visibility='visible';
document.getElementById('large2').style.visibility='hidden';
}

get_big3() {
document.getElementById('thumbnailwrapper').style.visibility='hidden';
document.getElementById('large3').style.visibility='visible';
}
get_thumbs3() {
document.getElementById('thumbnailwrapper').style.visibility='visible';
document.getElementById('large3').style.visibility='hidden';
}

</script>

There must be a better way! I imagine it’s not that difficult, I just can’t to get a grasp on it yet. Thanks in advance.

  • 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-29T07:36:04+00:00Added an answer on May 29, 2026 at 7:36 am

    There are lots of ways to tackle this. The first and most obvious that comes to my mind is simply to pass a number into a single function which determines the image id to modify:

    function get_thumbs(id) {
      document.getElementById('thumbnailwrapper').style.visibility='visible';
      document.getElementById('large' + id).style.visibility='hidden';
    }
    
    <div id="largeimage_wrapper">
        <div id="large1"><img src="thumbnail1.jpg" onClick="get_thumbs(1);"/></div>
        <div id="large2"><img src="thumbnail2.jpg" onClick="get_thumbs(2);"/></div>
        <div id="large3"><img src="thumbnail3.jpg" onClick="get_thumbs(3);"/></div>
        ...etc
    </div>
    

    … And the same thing for get_big().

    Alternatively you can use just one function that handles both conditions (big or thumbnail):

    function get_img(id, type) {
      if (type == 'big') {
        document.getElementById('thumbnailwrapper').style.visibility='hidden';
        document.getElementById('large' + id).style.visibility='visible';
      }
      else if (type == 'thumb') {
        document.getElementById('thumbnailwrapper').style.visibility='visible';
        document.getElementById('large' + id).style.visibility='hidden';
      }
      else return false;
    }
    

    And in the HTML:

    <div id="thumbnail_wrapper">
        <div id="thumbnail1"><img src="thumbnail1.jpg" onClick="get_img(1, 'big');"/></div>
        <div id="thumbnail2"><img src="thumbnail2.jpg" onClick="get_img(2, 'big');"/></div>
        <div id="thumbnail3"><img src="thumbnail3.jpg" onClick="get_img(3, 'big);"/></div>  
        ...etc
    </div>
    <div id="largeimage_wrapper">
        <div id="large1"><img src="thumbnail1.jpg" onClick="get_img(1, 'thumb');"/></div>
        <div id="large2"><img src="thumbnail2.jpg" onClick="get_img(2, 'thumb');"/></div>
        <div id="large3"><img src="thumbnail3.jpg" onClick="get_img(3, 'thumb');"/></div>
        ...etc
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string

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.