ユーザーがテキストを入力するためのフィールドを追加し、そのテキストを現在のdivに表示される新しい段落要素に追加しようとしています。
しかし、ユーザー入力の代わりに[オブジェクト]を受け取っています。これはまだ文字列値ではないためですか?オブジェクトを文字列に変換しようとしていますが、エラーが返され続けます。
function addText(){
var newParagraph = document.createElement('p');
var input = document.getElementById('userInput');
newParagraph.className = 'red';
newParagraph.appendChild(document.createTextNode(input));
document.getElementById('content').appendChild(newParagraph);
}
function getText(){
addText();
}
「userInput」の入力IDを使用しており、ボタンの関数を。でトリガーしていますonClick = "getText()"
。userInputテキストの代わりに[object]を返すことを除いて、スクリプトは正しく機能しています。
HTML:
<input type="text" id="userInput" />
<button id="button" onClick="getText()">Insert Text</button>