createElement()





js/script.js
  • function funMake() {
  •  for(var i=0; i<3; i++) {
  •   //create an element
  •   var el = document.createElement('p');
  •   //add an element
  •   document.getElementById('d1').appendChild(el);
  •  }
  •  document.getElementsByTagName('p')[0].innerHTML = 'Hello World!';
  •  document.getElementsByTagName('p')[1].innerHTML = 'Привет Мир!';
  •  document.getElementsByTagName('p')[2].innerHTML = 'To be or not to be?';
  • }
  • function funMake() {
  •  for(var i=0; i<3; i++) {
  •   //create an element
  •   var el = document.createElement('p');
  •   //add an element
  •   document.getElementById('d1').appendChild(el);
  •  }
  •  document.getElementsByTagName('p')[0].innerHTML = 'Hello World!';
  •  document.getElementsByTagName('p')[1].innerHTML = 'Привет Мир!';
  •  document.getElementsByTagName('p')[2].innerHTML = 'To be or not to be?';
  • }


index.html
  • <!DOCTYPE html>
  • <html>
  • <head>
  •  <meta charset="utf-8" />
  •  <title>New Page</title>
  •  <script src="js/script.js"></script>
  • </head>
  • <body>
  •  <div id="d1"></div>
  •  <script>
  •   funMake();
  •  </script>
  • </body>
  • </html>
  • <!DOCTYPE html>
  • <html>
  • <head>
  •  <meta charset="utf-8" />
  •  <title>New Page</title>
  •  <script src="js/script.js"></script>
  • </head>
  • <body>
  •  <div id="d1"></div>
  •  <script>
  •   funMake();
  •  </script>
  • </body>
  • </html>