user1656125
user1656125

Reputation: 107

How to instantiate an array when a user inputs?

When a user inputs text I want to create an array with the name of the inputted text and add it to array logNum. I've tried like this but it doesn't work:

logNum.push(var document.getElementById("buttonInput").value.toString()[]);

Any ideas?

Upvotes: 1

Views: 100

Answers (2)

ncank
ncank

Reputation: 956

i'm not sure i understood you, but try this:

logNum.push( window[ document.getElementById("buttonInput").value ] = [] );

first, we generate out array: window[ document.getElementById("buttonInput").value ] = [] this creates an array with a user-input name.

and remember that assignments always returns the value of right-hand-side. so new array will be returned.

then, push the returned value to logNum array.

Upvotes: 0

Denys Séguret
Denys Séguret

Reputation: 382150

Assuming you've yet created your logNum array, you can do this :

 logNum.push([document.getElementById("buttonInput").value]);

The [something] notation creates a new array whose first element is something.

Upvotes: 4

Related Questions