Advertisement
samimwebdev

JavaScript DOM (class-8)

Dec 18th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>DOM(Document object Model)</title>
  8. </head>
  9. <body class='body'>
  10.     <h1>DOM-The missing piece</h1>
  11.     <div class="container">
  12.         <ul id='nav'>
  13.  
  14.             <li class="first">First</li>
  15.             <li class='second'>Second</li>
  16.             <li class='three'>Third</li>
  17.             <li class="four">Four</li>
  18.  
  19.         </ul>
  20.  
  21.     </div>
  22.     <script>
  23.         //DOM
  24. //creating element
  25. //selecting element
  26. //Reading content from element
  27. //update or edit existing element
  28. //removing element
  29.  
  30. //selecting element
  31. //id (#)
  32. //class(.)
  33. //element
  34.  
  35. //selecting element
  36. // const h1Elm = document.querySelector('h1')
  37. // const containerElm = document.querySelector('.container')
  38. const navElm = document.querySelector('#nav')
  39. // const FirstNavElm = document.querySelector('#nav .first')
  40.  
  41. // //id , element, class
  42. // //getElementsByTagName
  43. // //getElementsByClassName
  44. // const navElmID = document.getElementById('nav')
  45.  
  46. // console.log(navElmID)
  47.  
  48. //reading content and writing content
  49. // h1Elm.textContent = 'Our new <em>Content</em>'
  50. // h1Elm.innerHTML = 'Our new <em>Content</em>'
  51.  
  52. // console.log(h1Elm.textContent)
  53.  
  54. //Traversing element
  55. // const firstNavElm = navElm.children[0]
  56. // console.log(
  57. //   firstNavElm.nextElementSibling.nextElementSibling.previousElementSibling
  58. // )
  59.  
  60. //parentElement (Immediate parent)
  61. //closest() (write your target)
  62. // console.log(firstNavElm.closest('.body'))
  63.  
  64. //ForEach couldn't applied on HTMLCollection(must be converted using Array.From())
  65.  
  66. //Removing Element
  67.  
  68. //using Target Element
  69. //firstNavElm.remove()
  70.  
  71. //Using parent
  72. // firstNavElm.parentElement.removeChild(firstNavElm)
  73.  
  74. //creating Element
  75. //<li class="four">Four</li>
  76. // const liElm = document.createElement('li')
  77.  
  78. // liElm.className = 'five'
  79. // liElm.textContent = 'Five'
  80.  
  81. //appendChild
  82. //prepend
  83. // navElm.prepend(liElm)
  84.  
  85. //insetAdjacentHTML
  86. //insetAdjacentElement
  87.  
  88. // navElm.insertAdjacentElement('beforeend', liElm)
  89.  
  90. // console.log(liElmHTML)
  91.  
  92. // const arrNavElm = Array.from(navElm.children)
  93. // arrNavElm.forEach((elm) => {
  94. //   elm.addEventListener('click', (e) => {
  95. //     console.log(e.target.textContent)
  96. //   })
  97. // })
  98.  
  99. //Event Delegation
  100. //performance
  101. navElm.addEventListener('click', (e) => {
  102.   if (e.target.className === 'five') {
  103.     console.log('you are clicking new added element')
  104.   }
  105.   console.log(e.target)
  106. })
  107.  
  108. const liElmHTML = '<li class="five">Five</li>'
  109. navElm.insertAdjacentHTML('beforeend', liElmHTML)
  110.  
  111. //Event capturing
  112. //Event Bubbling
  113.  
  114.     </script>
  115. </body>
  116. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement