previousSibling





js/script.js
  • function funMake() {
  •  var val0 = document.getElementById('d1');
  •  val0.innerHTML = '<h3>' + document.getElementById('p1').previousSibling.innerHTML + '</h3>';
  •  var val1 = document.getElementById('d2');
  •  val1.innerHTML = '<h3>' + document.getElementById('p2').previousSibling.innerHTML + '</h3>';
  • }
  • function funMake() {
  •  var val0 = document.getElementById('d1');
  •  val0.innerHTML = '<h3>' + document.getElementById('p1').previousSibling.innerHTML + '</h3>';
  •  var val1 = document.getElementById('d2');
  •  val1.innerHTML = '<h3>' + document.getElementById('p2').previousSibling.innerHTML + '</h3>';
  • }


index.html
  • <!DOCTYPE html>
  • <html>
  • <head>
  •  <meta charset="utf-8" />
  •  <title>New Page</title>
  •  <script src="js/script.js"></script>
  • </head>
  • <body>
  •  <div>
  •   <p>Hello World!</p>
  •   <p id="p1">Привет Мир!</p>
  •   <p>To be or not to be?</p>
  •  </div>
  •  <div id="d1"></div>
  •  <div>
  •   <p>Hello World!</p><p id="p2">Привет Мир!</p><p>To be or not to be?</p>
  •  </div>
  •  <div id="d2"></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>
  •   <p>Hello World!</p>
  •   <p id="p1">Привет Мир!</p>
  •   <p>To be or not to be?</p>
  •  </div>
  •  <div id="d1"></div>
  •  <div>
  •   <p>Hello World!</p><p id="p2">Привет Мир!</p><p>To be or not to be?</p>
  •  </div>
  •  <div id="d2"></div>
  •  <script>
  •   funMake();
  •  </script>
  • </body>
  • </html>