如何将textarea内容推送到数组中

时间:2021-07-12 21:13:50

I have a textarea that users can write in. Is it possible to push that into an array? When I console.log it I get "[object HTMLTextAreaElement]" -- is this something that can be done?

我有一个用户可以写入的textarea。是否可以将其推入数组?当我在console.log中时,我得到“[object HTMLTextAreaElement]” - 这是可以做到的吗?

<textarea id="sermon" cols="100" rows="20">
Write here...
</textarea>

</div> <button id="newContent"><a href='#' onclick='downloadCSV({ filename: "card-data.csv" });'>Download your text to a CSV.</a></button>


var myArray = [];
myArray.push(sermon);
console.log(myArray.join());

3 个解决方案

#1


0  

Try with toString() function, like this :

尝试使用toString()函数,如下所示:

Write here...
</textarea>

</div> <button id="newContent"><a href='#' onclick='downloadCSV({ filename: "card-data.csv" });'>Download your text to a CSV.</a></button>


var myArray = [];
myArray.push(sermon.toString());
console.log(myArray.join());

#2


0  

HTML should be something like this:

HTML应该是这样的:

<textarea id="sermon" cols="100" rows="20">

</textarea>

<button onclick="pushData()">Add to Array</button>

Javascript should be like this:

Javascript应该是这样的:

function pushData(){
   var text= document.getElementById("sermon").value;

    var array = [];

    array.push(text); 

    console.log(array.toString());
}

#3


0  

You may find convenient not to call the javascript inline, but bind some action to the click event.

您可能会觉得不方便调用内联的javascript,但将一些操作绑定到click事件。

Html

<textarea id="sermon" cols="100" rows="20">
Write here...
</textarea>

<button id="newContent">Download your text to a CSV.</button>

Javascript

// wait for your page to be loaded
window.onload = function() {
  // declare your empty variables
  var myArray = [];
  var sermon;

  // find the button element
  var button = document.getElementById('newContent');

  // attach some code to execute on click event
  button.onclick = function() {
    // put a call to your download function here

    // get the new value of the text area
    sermon = document.getElementById('sermon').value;

    myArray.push(sermon);
    console.log(myArray.join());
}  
};

#1


0  

Try with toString() function, like this :

尝试使用toString()函数,如下所示:

Write here...
</textarea>

</div> <button id="newContent"><a href='#' onclick='downloadCSV({ filename: "card-data.csv" });'>Download your text to a CSV.</a></button>


var myArray = [];
myArray.push(sermon.toString());
console.log(myArray.join());

#2


0  

HTML should be something like this:

HTML应该是这样的:

<textarea id="sermon" cols="100" rows="20">

</textarea>

<button onclick="pushData()">Add to Array</button>

Javascript should be like this:

Javascript应该是这样的:

function pushData(){
   var text= document.getElementById("sermon").value;

    var array = [];

    array.push(text); 

    console.log(array.toString());
}

#3


0  

You may find convenient not to call the javascript inline, but bind some action to the click event.

您可能会觉得不方便调用内联的javascript,但将一些操作绑定到click事件。

Html

<textarea id="sermon" cols="100" rows="20">
Write here...
</textarea>

<button id="newContent">Download your text to a CSV.</button>

Javascript

// wait for your page to be loaded
window.onload = function() {
  // declare your empty variables
  var myArray = [];
  var sermon;

  // find the button element
  var button = document.getElementById('newContent');

  // attach some code to execute on click event
  button.onclick = function() {
    // put a call to your download function here

    // get the new value of the text area
    sermon = document.getElementById('sermon').value;

    myArray.push(sermon);
    console.log(myArray.join());
}  
};