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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:38:01+00:00 2026-06-12T19:38:01+00:00

I am trying to add elements to an array via a form. I am

  • 0

I am trying to add elements to an array via a form. I am using the unshift() method. The code below doesn’t work and I would like to know why.

<form>
<input id="input"> </input>
<input type = "button" id="button"> Click me </input>
</form>


<script>

var input = document.getElementById("input").value;
var button = document.getElementById("button");

var myArray = [];
myArray.unshift(input);



button.onclick = function alerted (){
alert(myArray);
};


</script>
  • 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-12T19:38:03+00:00Added an answer on June 12, 2026 at 7:38 pm

    Your quoted code runs immediately when the page is loaded. The form field won’t have anything in it then, so its value will be ''. When you alert that, the default toString operation on the array will result in '' and the alert will be blank.

    You want to run your unshift code in response to a user event, such as the button being clicked, rather than right away. You can do that by setting input to be the element (remove .value from that line) and then moving your line with unshift into the function you’re assigning to onclick, adding the .value there:

    button.onclick = function alerted (){
        myArray.unshift(input.value);
        alert(myArray);
    };
    

    Other notes:

    1. You never write </input>. Normally you don’t close input tags at all. If you’re writing XHTML (you probably aren’t), you’d put the / within the main input tag like this: <input id="input" />. But again, you’re probably not writing XHTML, just HTML.

    2. The value (caption) of an input button goes in its value attribute, not content within opening and closing tags. (You would use opening and closing tags with the button element, not input.)

    Taking all of that together, here’s a minimalist update: Live copy | source

    <form>
    <input id="input"><!-- No ending tag -->
    <input type = "button" id="button" value="Click me"><!-- No ending tag, move value where it should be -->
    </form>
    <script>
    
    var input = document.getElementById("input"); // No .value here
    var button = document.getElementById("button");
    
    var myArray = [];
    
    button.onclick = function alerted (){
        myArray.unshift(input.value); // Moved this line, added the .value
        alert(myArray);
    };
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add all the elements in array using push . then
I'm trying to add filters for my form elements $this->add(array( 'name' => 'name', 'attributes'
I am trying to add elements to a scroll view using this code: int
In Python, I'm trying to add elements from an array into a string using
I'm trying to add an array of labels to a panel in my Form.
I am trying to print out all my elements of the array and add
I'm trying to add elements to an array after subsequent trials, but so far
I'm trying to add elements to a sub array of the sub array where
I'm trying to add elements into an Associative Array, which is colors = []
Im trying to add elements to a dict list (associative array), but every time

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.