admin管理员组文章数量:1318973
How would I populate an array from text in a <textarea>
input?
My end goal is to take a list of names and then insert it into an array to perform some operations. The names would be separated by lines, like this:
bob
tim
sally
and then added to the array. To be honest, I don't know where to start other than adding the <textarea>
and creating a blank array. I was reading other posts, but they were in jQuery or some other language that I don't know yet. My understanding is that I have to split the content somehow?
How would I populate an array from text in a <textarea>
input?
My end goal is to take a list of names and then insert it into an array to perform some operations. The names would be separated by lines, like this:
bob
tim
sally
and then added to the array. To be honest, I don't know where to start other than adding the <textarea>
and creating a blank array. I was reading other posts, but they were in jQuery or some other language that I don't know yet. My understanding is that I have to split the content somehow?
-
var array = textareaElement.value.split('\n');
– ibrahim mahrir Commented Jul 16, 2017 at 0:04 -
1
If you want to remove surounding spaces use
map
andtrim
. If you want to remove empty strings usefilter
:var array = textareaElement.value.split('\n').map(e => e.trim()).filter(e => e);
. – ibrahim mahrir Commented Jul 16, 2017 at 0:07
4 Answers
Reset to default 5It's pretty straightforward:
var textarea = document.querySelector('textarea#names');
var textareaValue = textarea.value; // 'bob\ntim\nsally';
var arr = textareValue.split('\n');
split() takes string and cuts it into an array every given character (or string).
\n
is new line character.
Edit: Answer to questions asked in ment below:
var names = [];
var textarea = document.querySelector('textarea#names');
function saveNames() {
names = textarea.value.split('\n');
}
textarea.addEventListener('blur', saveNames, false);
// or this:
// textarea.addEventListener('keyup', saveNames, false);
You use events. You can use blur event or even keyup event. Basically you listen for (events) something to happen (like textarea loose focus or user press keyboard button), and then do something (read textarea value and save names to array).
This is of course pure js, but if you want to use jquery, then you need to go to jquery reference and see how to use events with jquery.
Let's assume you have a textarea and after typing each name you hit return. This should get you started:
<textarea id="textToArray" style="width:300px; height:120px;"></textarea>
<button onclick="toArray()">To Array</button>
<div id="arrayOutput"><div/>
<script type="text/javascript">
function toArray( ) {
var textAreaElement = document.getElementById('textToArray');
var namesArray = textAreaElement.value.trim().split('\n');
console.log( namesArray );
document.getElementById('arrayOutput').innerText = JSON.stringify(namesArray);
}
</script>
If your words are s Here you go:
<textarea name="example" id="example" cols="30" rows="10">
bob
tim
sally
</textarea>
const textareaContent = document.getElementById('example').value.split('\n');
demo
https://codepen.io/nicholasabrams/pen/weZaOj
try this:
<html>
<body>
<textarea id="textArea"></textarea>
<button onclick="textToArray()">to array</button>
<!--show array result-->
<p id="result"></p>
</body>
</html>
<script>
function textToArray() {
//assign value of textarea to mytext
var myText = document.getElementById("textArea").value;
//split mytext by nextline = "\n"
var myArray = myText.split("\n");
//to check the result
var result = '';
for(i=0;i<myArray.length;i++)
{
result += myArray[i]+"<br>";
}
document.getElementById("result").innerHTML = result;
}
</script>
本文标签: htmlPopulate an array from lttextareagt in JavaScriptStack Overflow
版权声明:本文标题:html - Populate an array from <textarea> in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742055556a2418270.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论