My TIPs for the programing

プログラミング学習記録

ノードの追加

appendChildメソッドを用いて特定の親ノードの最後の子ノードとしてノードを追加する


parent.appendChild(node);

 parent: 親ノード

node: 追加するノード

 

適用例

<script>

const lists = document.getElementById("lists")
//ulの取得。あとのリスト追加用
const target = document.getElementById("btn");
//送信ボタンの取得。イベントのターゲットです
target.addEventListener("click",function(){

const form = document.getElementById("name");
//formの取得
const list = document.createElement("li")
//liタグの生成
list.textContent=form.value;


lists.appendChild(list);
//htmlのlistsに
//listにformのテキストデータを持たせる
form.value ="";
//フォームのリセット
console.log(list)

},false);

</script>