3
4

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

4/3 Javascript コードレシピ集 学習

Last updated at Posted at 2019-04-03

jsの本を借りたので、毎日継続して定着させていきます。

クリックしたら要素を複製

アンケートのフォームを追加する時に応用出来るはず。

main.js
// 要素を複製 複製したい要素.cloneNode(真偽値) appendChild等と共に使うこと trueを外すと複製されない

// my-clone-boxを取得
const cloneBox = document.getElementById('my-clone-box');

// my-clone-boxをクリックすると発火
cloneBox.addEventListener('click', () =>
{
  // 親要素のcontainerを取得
  const container = document.querySelector('.container');

  // my-clone-boxを複製する
  const clone = cloneBox.cloneNode(true);

  // 複製したmy-clone-boxを、containerの末尾に挿入する
  container.appendChild(clone);
});
js.html
<div class="container">
  <div id="my-clone-box">クリックするとこの要素を複製します</div>
</div>

参考
https://www.imamura.biz/blog/26914

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?