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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:03:01+00:00 2026-05-24T19:03:01+00:00

had a look for a guide on this but couldn’t really find anything specific

  • 0

had a look for a guide on this but couldn’t really find anything specific on it, and thought you guys could help.

I have a javascript file which performs various tasks onLoad. Currently I have 5 buttons in an HTML page but what I would like to do is to is read from an array in the js file and dynamically create a number of buttons depending what was in the array.

Similarly I want the buttons on click to be ‘listened’ for (alter an array in the js)

I have an idea that I want to read the array elements and set them as the button IDs and create them. Likewise I want to listen to what button has been clicked by its ID and have it alter an array. But how do I actually go about that process?

Cheers

  • 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-24T19:03:03+00:00Added an answer on May 24, 2026 at 7:03 pm

    Something like this…?

    <html>
      <head>
        <script type="text/javascript">
          var arrayToModify = [];
          window.onload = function () {
            var i, buttonsToCreate, buttonContainer, newButton;
            buttonsToCreate = ['button1','button2','button3','button4','button5'];
            buttonContainer = document.getElementById('this_element_contains_my_buttons');
            for (i = 0; i < buttonsToCreate.length; i++) {
              newButton = document.createElement('input');
              newButton.type = 'button';
              newButton.value = buttonsToCreate[i];
              newButton.id = buttonsToCreate[i];
              newButton.onclick = function () {
                alert('You pressed '+this.id);
                arrayToModify[arrayToModify.length] = this.id;
              };
              buttonContainer.appendChild(newButton);
            }
          };
        </script>
      </head>
      <body>
        <div id='this_element_contains_my_buttons'></div>
        <input type='button' onclick='alert(arrayToModify);' value='Show the buttons I have clicked' />
      </body>
    </html>
    

    EDIT: I have just added some functionality to track button presses in an array, as requested in the comment above

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

Sidebar

Related Questions

I've had a look at code.google.com, but I didn't find anywhere how to programatically
I've had a look around on StackOverlow but haven't been able to find a
i had a look on how to do this but it looks too complicated.
I had a look at other stackoverflow questions and couldn't find one that asked
I’ve had a good look and can’t seem to find out how to select
I recently had to take a quick look at Adobe InDesign server. In this
This is a slightly.. vain question, but BuildBot's output isn't particularly nice to look
I had a look at this previous question however this doesn't seem to work
I've had a look round similar articles such as this one and I can't
Had a look over SO but I can't see any threads which address my

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.