1

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?

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! So the new array doesn't have a name.. just an index within logNum?
Yes. There is no point in giving a name to a variable if you want to get it from an array later.
0

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.

Comments

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.