简体   繁体   中英

Storing Values in an Array from Textbox in Javascript

I've been trying to experiment in a way in which i can store values entered from a textbox into an array.(using JavaScript or JQuery)

I understand I can do it this way:

var array = []; var input = document.getElementById('input_id').value; array.push(input);

But the functionality that I want to achieve is to be able to give the user an option to choose which array they want to save their input to.

Summary:

  • How can a user choose which array to store the input text to

Try to use an object with keys as the options you want to give to the user and its value as array.

{'opt1':[],'opt2':[],...}

When the user selects his option get that array and obj['opt'].push(inputValue) . If the option does not exist then initialize a new array obj['someKey'] = [] and start pushing into it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM