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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:45:35+00:00 2026-06-17T11:45:35+00:00

I have made three boxes and each box contains a button. When I click

  • 0

I have made three “boxes” and each box contains a button. When I click the button, box hiding, when click again, box appears.

This is my html code:

<div id="SC1_A_"> <!-- BOX -->
<div id="SC1_B_" onClick="SC1();" class="something">&nbsp;</div> <!-- BUTTON -->
</div>
<div id="SC2_A_">
<div id="SC2_B_" onClick="SC2();" class="something">&nbsp;</div>
</div>
<div id="SC3_A_">
<div id="SC3_B_" onClick="SC3();" class="something">&nbsp;</div>
</div>

This is my javascript code:

<script type="text/javascript">    
function SC1(){

  var SC1_A = document.getElementById('SC1_A_);
  var SC1_B = document.getElementById('SC1_B_);

  if (SC1_A.style.display == 'block' || SC1_A.style.display == ''){
      SC1_A.className      = 'something';
      SC1_B.className      = 'something else';}   
else {SC1_A.className      = 'something else';
      SC1_B.className      = 'something';}
     }
     }
</script>

The example above works, but I have to make three similar scripts for each button. So I though to make something like this script below, using for loop. As you can imagine it didn’t work. Any idea how can I make it work???

<script type="text/javascript">

for (i=1; i<10; i++){

function SCi(){

  var SCi_A = document.getElementById('SC'+i+'_A_');
  var SCi_B = document.getElementById('SC'+i+'_B_');

  if (SCi_A.style.display == 'block' || SCi_A.style.display == ''){
      SCi_A.className      = 'something';
      SCi_B.className      = 'something else';}   
else {SCi_A.className      = 'something else';
      SCi_B.className      = 'something';}
     }
     }
</script>

Please don’t down-vote if you think question is too easy, but just give me your help here!!! Thank you 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-06-17T11:45:36+00:00Added an answer on June 17, 2026 at 11:45 am

    You’re on the right track, you just need to learn the right syntax for what you are trying to express:

    var SC = [];
    

    First off, to have a lot of different functions, so instead of attempting to name them differently (which you were trying to do), we are going to just store each function in a different index in the SC array.

    for (var i = 1; i < 10; i++) {
        SC[i] = (function () {
            var SC_A = document.getElementById('SC' + i + '_A_');
            var SC_B = document.getElementById('SC' + i + '_B_');
    
            return function () {
                if (SC_A.style.display === 'block' || SC_A.style.display === '') {
                    SC_A.className = 'something';
                    SC_B.className = 'something else';
                } else {
                    SC_A.className = 'something else';
                    SC_B.className = 'something';
                }
            }
        })();
    }
    

    Now, to call these functions you would do SC[1](), SC[2](), … So you can either put that in each onclick in your HTML, or you could bind the events from the javascript.


    Edit: I forgot to mention this because it isn’t directly related to the syntax of the code, but the calls to ‘document.getElementByIdwill not work until the document is fully loaded. So if you just put the script directly between to` tags it won’t work. You have two choices. You either can keep the current code, but run it when the page loads. Or, you could restructure the code like this:

    var SC = [];
    for (var i = 1; i < 10; i++) {
        SC[i] = (function (i) {
            return function () {
                var SC_A = document.getElementById('SC' + i + '_A_');
                var SC_B = document.getElementById('SC' + i + '_B_');
    
                if (SC_A.style.display === 'block' || SC_A.style.display === '') {
                    SC_A.className = 'something';
                    SC_B.className = 'something else';
                } else {
                    SC_A.className = 'something else';
                    SC_B.className = 'something';
                }
            }
        })(i);
    }
    

    What’s happening here is you are calling document.getElementById every time the button is clicked, instead of just once when the function is created. Slightly less efficient, but it works.

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

Sidebar

Related Questions

I have a webpage which has three green boxes, each on their own line.
Recently i have made three Cake Apps and all three share this problem. The
I have made an html wizzard for making order. There are four steps, in
I have a slider I made: http://dranz1r.com/index.html When you slide right for example, there
I have a form made in C# that has two text boxes and a
I know a bit of PHP and so also HTML/CSS, and I have made
I have three select boxes on a page and I want to keep the
My code is as follows: <script> $(document).ready(function() { var tagCounter=0; $(#tag-add-button).click(function () { var
I am new to django and have made a method:GET form with checkboxes/text boxes
I have a select box of TYPES that each type has their own PARAMETERS.

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.