Commits

Keiji Matsuzaki  committed c468a24

create question_2/sample_2

  • Participants
  • Parent commits 75b5da6

Comments (0)

Files changed (2)

File question_2/sample_2/index.html

+<html>
+  <head>
+    <meta charset="UTF-8">
+    <title>inner HTML</title>
+  </head>
+  <body>
+    <div>
+      <h1>innerHTML</h1>
+      <button id="insert-element">insert 100 element</button>
+    </div>
+    <hr/>
+    <div id="target">
+    </div>
+    <script type="text/javascript" src="main.js"></script>
+  </body>
+</html>

File question_2/sample_2/main.js

+(function(window){
+  var MyApp = {
+    init: function(){
+      $.target = document.getElementById("target");
+      $.button = document.getElementById("insert-element");
+      $.button.addEventListener("click", $.insert, false);
+    },
+    cleanup: function(){
+      if($.target.childNodes != null){
+        while($.target.firstChild){
+          $.target.removeChild($.target.firstChild);
+        }
+      }
+    },
+    create: function(num){
+      var result = [];
+      for(var i = 0; i < num; i++){
+        result.push('<p>Element: ' + i + '</p>');
+      }
+      return result.join('');
+    },
+    insert: function(){
+      $.cleanup();
+      var elements = $.create(100);
+      $.target.innerHTML = elements;
+      console.log(elements);
+    }
+  };
+  window.MyApp = window.$ = MyApp;
+})(window);
+
+$.init();