2016-05-29 5 views
0

У меня есть текстовое поле, в котором пользователи могут писать. Можно ли вставить это в массив? Когда я console.log, я получаю «[object HTMLTextAreaElement]» - это что-то, что можно сделать?Как добавить содержимое textarea в массив

<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()); 

ответ

0

Try с функцией 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()); 
0

HTML должно быть что-то вроде этого:

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

</textarea> 

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

Javascript должен быть так:

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

    var array = []; 

    array.push(text); 

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

Спасибо! Потрясающие. Кажется, это работает. – NJeffries

0

Возможно, вам удобно не вызывать встроенный javascript, но привязать какое-то действие к событию клика.

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()); 
} 
}; 
+0

Smart. Это хорошо работает. Спасибо за помощь. – NJeffries

+0

О, спасибо за комментирование. Это очень полезно. – NJeffries

+0

Рад, что я могу помочь! –

Смежные вопросы