Advertisement
samimwebdev

class-09-DOM

Oct 13th, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //selecting element
  2. // const headingId = document.getElementById('heading-1')
  3. // const headingTag = document.getElementsByTagName('h1')
  4.  
  5. //selection by class/ Tag querySelector, querySelectorAll
  6. const headingTag = document.querySelector('h1')
  7. const headingClass = document.querySelector('.heading-1')
  8. const headingId = document.querySelector('#heading-1')
  9. const liElms = document.querySelectorAll('li')
  10. const ulElm = document.querySelector('ul')
  11. const containerElm = document.querySelector('.container')
  12.  
  13. // liElms.forEach(elm => console.log(elm))
  14. // console.log(liElms[1].textContent)
  15. // console.log(ulElm.childNodes)
  16. // console.log(ulElm.children[1].textContent)
  17. //changing element
  18. // ulElm.children[1].textContent = 'Modified'
  19. // ulElm.children[1].innerHTML = '<i>Modified</i>'
  20. // console.log(ulElm.children[1].textContent)
  21.  
  22. const firstLi = ulElm.children[0]
  23.  
  24. // console.log(
  25. //   firstLi.nextElementSibling.nextElementSibling.previousElementSibling
  26. //     .parentElement
  27. // )
  28.  
  29. //closest vs parent element
  30. // console.log(
  31. //   firstLi.nextElementSibling.nextElementSibling.previousElementSibling.parentElement.closest(
  32. //     '.container'
  33. //   )
  34. // )
  35. //Inserting Element
  36. //Hard way
  37. // const newLi = document.createElement('li')
  38. // newLi.className = 'fourth'
  39. // newLi.textContent = 'Summary'
  40. // ulElm.appendChild(newLi)
  41.  
  42. //Easiest way
  43. const liElm = '<li class="fourth">Summary</li>'
  44. ulElm.insertAdjacentHTML('beforeend', liElm)
  45.  
  46. //Removing element
  47. //using parent
  48. // ulElm.removeChild(firstLi)
  49. // firstLi.parentElement.removeChild(firstLi)
  50. //using direct children
  51. // firstLi.remove()
  52.  
  53. // firstLi.addEventListener('click', evt => {
  54. //   console.log(evt.target.textContent)
  55. // })
  56.  
  57. //event delegation
  58. //problem solution
  59. //performance benefit
  60. // ulElm.addEventListener('click', evt => {
  61. //   if (evt.target.textContent === 'Summary') {
  62. //     console.log(evt.target.textContent)
  63. //   }
  64. // })
  65.  
  66. //capturing phase
  67. //target vs current target
  68. firstLi.addEventListener(
  69.   'click',
  70.   e => {
  71.     alert(e.currentTarget.tagName)
  72.   },
  73.   true
  74. )
  75. ulElm.addEventListener(
  76.   'click',
  77.   e => {
  78.     //alert(e.currentTarget.tagName)
  79.     //stop bubbling
  80.     // e.stopPropagation()
  81.     alert(e.currentTarget.tagName)
  82.   },
  83.   true
  84. )
  85.  
  86. containerElm.addEventListener(
  87.   'click',
  88.   e => {
  89.     alert(e.target.tagName)
  90.     alert(e.currentTarget.tagName)
  91.   },
  92.   true
  93. )
  94.  
  95. //Bubbling Phase
  96. // firstLi.addEventListener('click', e => {
  97. //   alert(e.target.tagName)
  98. //   alert(e.currentTarget.tagName)
  99. // })
  100.  
  101. // ulElm.addEventListener('click', e => {
  102. //   //alert(e.currentTarget.tagName)
  103. //   //stop bubbling
  104. //   // e.stopPropagation()
  105. //   alert(e.currentTarget.tagName)
  106. // })
  107.  
  108. // containerElm.addEventListener('click', e => {
  109. //   alert(e.currentTarget.tagName)
  110. //   // alert(e.currentTarget.tagName)
  111. // })
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement