Javascript DOM Element append child

Description

Javascript DOM Element append child

View in separate window


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>NodeList</title>
<script type="text/javascript">
window.onload=function() {//  w ww  .  ja  v  a2s.  com
   let imgs = document.getElementsByTagName('img');
   console.log(imgs.length);
   let p = document.createElement("p");
   let img = document.createElement("img");
   img.src="image4.png";
   p.appendChild(img);
   
   let paras = document.getElementsByTagName('p');
   paras[0].parentNode.appendChild(p);

   console.log(imgs.length);
}
</script>
</head>
<body>
<p><img src="image1.png" alt="Orchid from MBG 2009 orchid show" /></p>
<p><img src="image2.png" alt="Orchid from MBG 2009 orchid show" /></p>
<p><img src="image3.png" alt="Orchid from MBG 2009 orchid show" /></p>
</body>
</html>



PreviousNext

Related