简体   繁体   中英

Get the input data from textarea of HTML and store that values in an array

I want to extract the multiple data from textbox. Suppose I enter following data into text field separated by space.

11 22 33 44 55 66

then it should separate the data from textbox and store that data in an array, say a. If I say, I want to display value of first element array ie a[0], then it should display 11 and so on.

Thank you !

Use str.split([separator[, limit]])

The split() method splits a String object into an array of strings by separating the string into substrings.

 var text = document.getElementById('ta').value; console.log(text.split(' ')); 
 <textarea name="ta" id="ta" cols="30" rows="10">11 22 33 44 55 66</textarea> 

Just use the appropriate method split :->

String string = "11 22 33 44 55 66";
String[] parts = string.split(" ");

@Rayon's answer is correct and uses core javascript.

If you'd like to do it in jQuery you can chain commands like so:

var a = $('ta').val().split(' ');

If you then want to get the first number of the array, you could do:

a[0];

Where text area is:

<textarea name="ta" id="ta" cols="30" rows="10">11 22 33 44 55 66</textarea>

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