1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[備忘録] jsで任意のulにliを追加する方法

Posted at
html index.html
<ul id=list>
 <!-- ここにJSでリストアイテムを追加したい! -->
</ul>
js script.js
// 親要素を指定する
var list = document.getElementById('list');

// 追加する要素を作成
var li = document.createElement('li');
li.innerHTML = 'list_item';

// 末尾に追加
list.appendChild(li);

ボタンを押した時に〜〜〜みたいな挙動

html index.html
<button id=btn> ここ!マダガスカル! </button>
<ul id=list>
 <!-- ここにJSでリストアイテムを追加したい! -->
</ul>
js script.js
const add_btn = document.getElementById('btn');
var ul = document.getElementById('list');

add_btn.addEventLister('click', function(){
    var li = document.createElement('li');
    li.innerHTML = 'マダガスカル!!!'
    list.appendChild(li);
}};
1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?