Advertisement
Iv555

Untitled

Mar 31st, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. // Inline JS in HTML Tag:
  2. <button onclick="console.log('Hello, DOM!')">Click Me</button>
  3.  
  4. // Write in the end of the <body></body>
  5. // HTML: <script> JS Here </script>
  6.  
  7. // DOM Methods:
  8.  
  9. // Getting the element by Tag Name.
  10. // Using [0] because returned result is array with all 'h1' tags and we get the first element.
  11. let h1Element = document.getElementsByTagName('h1')[0]
  12.  
  13. // Getting the second element from array by Tag Name.
  14. let secondLi = document.getElementsByTagName('li') [1]
  15.  
  16. //Getting element by Class Name selector:
  17. let otherElement = document.getElementsByClassName('class-name')
  18. // This all selectors returns HTMLCollection
  19.  
  20. // Getting element by ID selector:
  21. let element = document.getElementById('ID')
  22. // This selector returns an element
  23.  
  24. // Getting first element by CSS selector:
  25. let elementByCssSelector = document.querySelector('.class-name')
  26.  
  27. // Getting all elements by CSS selector/selectors:
  28. let elementsByCssSelector = document.querySelectorAll('.class-name')
  29. // This two selectors returns NodeList
  30.  
  31. // Getting all elements by attribute 'name' usually in input tag.
  32. let el = document.getElementsByName('fname')
  33. let num = document.getElementsByName('animal').length
  34.  
  35. // To convert NodeList or HTMLCollection to Array may using Array.from()
  36.  
  37. // DOM properties:
  38.  
  39. // Getting the value of input field:
  40. const textInput = document.getElementById('text-input')
  41. // Changing value:
  42. textInput.value = "Changed value"
  43. // If value is digit as string. We can change it:
  44. let number = Number(textInput.value)
  45. // Changing content of NON self closing tag:
  46. h1Element.textContent = "Changed content"
  47. // Add nested tag to HTML and remove all existing nested tags:
  48. h1Element.innerHTML = '<p>Nested paragraph</p>'
  49. // Changing inline (in tag) style.
  50. h1Element.style.backgroundColor = 'red'
  51. // Getting attribute from element:
  52. someElement.getAttribute('attributeName')
  53. // Setting attribute to the element:
  54. newAnchor.setAttribute('href', '#')
  55.  
  56. // DOM hierarchy manipulation
  57.  
  58. // Getting parent element (Node):
  59. let h1Parent = h1Element.parentElement
  60. // We can get multilevel parent element (Node):
  61. let grandparent = h1Element.parentElement.parentElement
  62. // Getting childNodes elements:
  63. let child = h1Element.children
  64. // All methods return collection !
  65.  
  66. // Getting next brother:
  67. let nextBrother = h1Element.nextSibling
  68. // Getting previous brother:
  69. let previousBrother = h1Element.previousSibling
  70. // All methods return single element
  71.  
  72. // Child manipulation:
  73.  
  74. // Add child. It adds child as a last child:
  75. // It must created first and is necessary to attach "text" to it.
  76. h1Element.appendChild(paragraph)
  77. // Add child as a first child:
  78. h1Element.prepend(paragraph)
  79. // Remove child:
  80. // Is necessary to insert reference which we want to remove!
  81. h1Element.removeChild(paragraph)
  82. // Replace child:
  83. h1Element.replaceChild(newElement, oldElement)
  84.  
  85.  
  86. // Creating a new DOM element:
  87. let paragraph = document.createElement("p")
  88. // Attach text to the paragraph:
  89. paragraph.textContent = "New Paragraph"
  90.  
  91. // Remove DOM element:
  92. h1Element.remove()
  93.  
  94. // Add event listener:
  95. btnElement.addEventListener("click", functionReferences)
  96.  
  97. // Current target definition:
  98. // It always refers to the element to which the event handler has been attached
  99. function deleteHandler(e) {
  100. e.currentTarget
  101. }
  102.  
  103. // Parent element of event:
  104. function deleteHandler(e) {
  105. e.currentTarget.parentElement
  106. }
  107.  
  108. // Target definition:
  109. // Identifies the element on which the event occurred and which may be its descendant.
  110. function getTarget(e) {
  111. e.getTarget
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement