1
0

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 3 years have passed since last update.

【JavaScript】【DOM操作】appendChild( )でHTML要素を追加

Last updated at Posted at 2020-09-19

#書き方

[親要素].appendChild(追加要素);

#サンプルコード
以下のようなリストがあるとします。

example.html
<ul id="menuList">
    <li>coffee</li>
    <li>tea</li>
</ul>

↑に<li>greenTea</li>を以下のコードで追加します。

example.js

// 親要素
var menuList = document.getElementById('menuList');

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

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

■結果
親要素の最後の子要素として追加されます。

example.html
<ul id="menuList">
  <li>coffee</li>
  <li>tea</li>
  <li>greenTea</li>
</ul>

#参照
MDN web docs

1
0
0

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?