|
| 1 | +## DOM Methods |
| 2 | + |
| 3 | +- `getElementById`: This method retrieves an element from the document by its ID. |
| 4 | + |
| 5 | +- `querySelector`: This method retrieves the first element that matches a specified CSS selector. |
| 6 | + |
| 7 | +- `querySelectorAll`: This method retrieves all elements that match a specified CSS selector. |
| 8 | + |
| 9 | +- `getElementsByTagName`: This method retrieves all elements with a specified tag name. |
| 10 | + |
| 11 | +- `getElementsByClassName`: This method retrieves all elements with a specified class name. |
| 12 | + |
| 13 | + Here are some examples of how you might use these methods: |
| 14 | + |
| 15 | + ```js |
| 16 | + Copy codeconst element = document.getElementById('my-element'); |
| 17 | + |
| 18 | + const firstParagraph = document.querySelector('p'); |
| 19 | + |
| 20 | + const listItems = document.querySelectorAll('li'); |
| 21 | + |
| 22 | + const divs = document.getElementsByTagName('div'); |
| 23 | + |
| 24 | + const redElements = document.getElementsByClassName('red'); |
| 25 | + ``` |
| 26 | + |
| 27 | +Here is a list of some common DOM (Document Object Model) properties and methods that you can use to manipulate HTML elements: |
| 28 | + |
| 29 | +#### Properties |
| 30 | + |
| 31 | +- `innerHTML`: This property gets or sets the HTML content within an element. |
| 32 | +- `style`: This property gets or sets the inline style of an element. |
| 33 | +- `className`: This property gets or sets the class name of an element. |
| 34 | +- `id`: This property gets or sets the ID of an element. |
| 35 | +- `value`: This property gets or sets the value of an input element. |
| 36 | + |
| 37 | +#### Methods |
| 38 | + |
| 39 | +- `getAttribute`: This method gets the value of an attribute of an element. |
| 40 | +- `setAttribute`: This method sets the value of an attribute of an element. |
| 41 | +- `addEventListener`: This method adds an event listener to an element. |
| 42 | +- `removeEventListener`: This method removes an event listener from an element. |
| 43 | +- `appendChild`: This method adds a new child element to an element. |
| 44 | + |
| 45 | +Here are some examples of how you might use these properties and methods: |
| 46 | + |
| 47 | +```js |
| 48 | +Copy codeconst element = document.getElementById('my-element'); |
| 49 | + |
| 50 | +element.innerHTML = 'Hello, world!'; |
| 51 | + |
| 52 | +element.style.color = 'red'; |
| 53 | + |
| 54 | +element.className = 'highlight'; |
| 55 | + |
| 56 | +element.id = 'new-id'; |
| 57 | + |
| 58 | +const input = document.querySelector('input'); |
| 59 | +input.value = 'Hello, world!'; |
| 60 | + |
| 61 | +const attributeValue = element.getAttribute('data-attribute'); |
| 62 | + |
| 63 | +element.setAttribute('data-attribute', 'new value'); |
| 64 | + |
| 65 | +element.addEventListener('click', () => { |
| 66 | + console.log('Element was clicked!'); |
| 67 | +}); |
| 68 | + |
| 69 | +element.removeEventListener('click', myClickHandler); |
| 70 | + |
| 71 | +const newElement = document.createElement('p'); |
| 72 | +newElement.innerHTML = 'This is a new element'; |
| 73 | +element.appendChild(newElement); |
| 74 | +``` |
0 commit comments