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>
0

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

How do I remedy this ? I should have asked that in the original question too
@Taoist: See the second paragraph (I don't recall -- amazingly -- whether I added that after posting the answer).
1

DEMO

You need to a) get the value in the click and b) return false if you want the button to not submit. I changed to button. Alternative is <input type="button" value="click me" id="button" />

You may even want to empty and focus the field on click...

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


<script>

var input = document.getElementById("input"); // save the object
var button = document.getElementById("button");

var myArray = [];




button.onclick = function alerted (){
    myArray.unshift(input.value); // get the value
    alert(myArray);
    return false;
};


</script>​

Comments

0

You're not getting the new value in the onclick function.

Try this: http://jsfiddle.net/SeqWN/4/

var button = document.getElementById("button");
var i = document.getElementById("input");
var myArray = [];

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

7 Comments

This has nothing to do with scope.
He was getting the value of the input outside of the function. Whatever you want to call that.
Justin, input is accessible in that anonymous function even if it's not defined within it. var test = 'test'; (function somefunction(){ console.log( test ) })();
@azuz, I'm not sure I follow your point? Are you suggesting something be changed?
@aziz.punjani: Yes, but I think Justin's point is that by taking the value from the input's .value prior to the click event, it's going to be blank, because there's no value in the input yet. Justin's changed it so that the code takes the value of .value when the click occurs, after the user has had time to type something in.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.