In JavaScript, the Document Object Model (DOM) provides properties and methods for accessing and manipulating elements in an HTML or XML document. These properties and methods allow you to change the content and appearance of elements, listen for events, and perform other operations on elements.
Here are some examples of element properties and methods in JavaScript:
element.innerHTML
: This property gets or sets the HTML content of an element. For example, you can use it to change the text of an element:element.innerHTML = "Hello, world!";
element.style
: This property gets or sets the style of an element. For example, you can use it to change the color of an element:element.style.color = "red";
element.addEventListener()
: This method adds an event listener to an element. For example, you can use it to listen for a click event on an element:element.addEventListener("click", () => console.log("Clicked!"));
element.appendChild()
: This method adds a child element to an element. For example, you can use it to add a new element to the end of an element:element.appendChild(newElement);
These are just a few examples of the many properties and methods that are available for working with elements in the DOM. You can find a complete list of these properties and methods in the JavaScript documentation.
Here is a link to the JavaScript documentation on MDN Web Docs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript
You can use this link to access the documentation and browse the available functions.
Here are some more examples of using element properties and methods in JavaScript:
To select an element with the my-element
ID and change its color to red, you can use the style
property and the color
property:
// Select the element
const element = document.getElementById("my-element");
// Change the element's color
element.style.color = "red";
To select an element with the my-element
ID and add a new element as its child, you can use the appendChild
method:
// Select the element
const element = document.getElementById("my-element");
// Create a new element
const newElement = document.createElement("p");
newElement.innerHTML = "This is a new element.";
// Add the new element as a child of the selected element
element.appendChild(newElement);
To select an element with the my-element
ID and listen for a click event on it, you can use the addEventListener
method:
// Select the element
const element = document.getElementById("my-element");
// Add a click event listener to the element
element.addEventListener("click", () => console.log("Clicked!"));
These are just a few examples of how you can use element properties and methods in JavaScript.
I hope this helps! Let me know if you have any questions.