Introduction to JavaScript and DOM Manipulation

Tiempo de lectura: < 1 minuto

JavaScript is an essential programming language for web development. With it, you can make your websites interactive and dynamic by interacting with the Document Object Model (DOM). The DOM is an in-memory representation of your web page’s structure, and JavaScript allows you to access and modify elements in the DOM to create visual changes and responses to user actions.

Manipulating the DOM in JavaScript:

  • Accessing an Element by its ID:
// Get an element by its ID
const myElement = document.getElementById("my-id");

// Change the content of an element
myElement.innerHTML = "Hello, world!";
  • Modifying Classes and Styles:
// Add a class to an element
myElement.classList.add("new-style");

// Change the background color
myElement.style.backgroundColor = "blue";
  • Creating Elements and Adding Them to the DOM:
// Create a new <p> element
const newParagraph = document.createElement("p");
newParagraph.textContent = "This is a new paragraph.";

// Append it to the end of a parent element
document.body.appendChild(newParagraph);
  • Modifying Attributes:
// Change the src attribute of an image
const myhtml
Copy code

Manipulating the DOM in JavaScript allows you to create visual effects, change content, respond to events, and much more. With these examples, you can start experimenting and make your websites more interactive and engaging for users.

I hope you like it and find it useful! 😊

Leave a Comment