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

[質問]追加した要素にjQuelyでclassを追加する

Last updated at Posted at 2016-08-04

jsで要素を追加した後に、追加した要素がクリックされるとjQuelyでclassがつくようにしたいのですがどうしたらいいのでしょうか?
以下のコードが動かなくて困っています。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>テスト</h1>
    <div id="test"></div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>
    (function(){
      'use strict';
      
        function show(){
        var div_element=document.createElement("div");
        div_element.id="hogehoge";
        div_element.innerHTML='あいうえお';
        var parent_object=document.getElementById('test');
        parent_object.appendChild(div_element);
      }

      addEventListener('load', function(){
        show();
      });

      $("#hogehoge").on("click", function(){
      $("#hogehoge").toggleClass("hogehoge");
      });

    })();
    </script>

  </body>
</html>
0
0
2

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
0
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?