Code Monkey home page Code Monkey logo

flw1-u1l6-23-24-student-exercises's Introduction

Lesson 1.6: DOM Manipulation, Part 1


Table of Contents

  1. Storing HTML Elements
  2. DOM Manipulation
  3. Updating Images

Quick Points:

  • DOM (Document Object Model): It's a blueprint or tree structure of a webpage.
  • Relation between HTML & JavaScript: HTML documents are transformed into the DOM. With JavaScript, we can manipulate this DOM.

1. Storing HTML Elements

How to Access HTML Elements:

  • Use the document object, representing the webpage.
  • Access specific elements using selectors.

Code Example:

let paragraph = document.querySelector("p");

Selectors:

  1. By tag name: document.querySelector("div")
  2. By class: document.querySelector(".my-div")
  3. By ID: document.querySelector("#myDiv")

2. DOM Manipulation

Basics:

  • Property: A characteristic or attribute in JavaScript that can be assigned a value.
  • innerHTML is a common property used to get or set content of an element.

Code Example:

let button = document.querySelector("button");
let header = document.querySelector("h1");

button.addEventListener("click", function() {
    header.innerHTML = "This is important!";
});

Event Types:

Common event types for .addEventListener:

  • "click"
  • "mouseover"
  • "mouseout"
  • "keydown"
  • "keypress"

3. Updating Images

How to Update Images:

  • <img> tags have a src attribute, which can be updated with JavaScript using the src property.

Code Example:

selector.src = "url";

Use this README as a quick reference. Practice makes perfect, so continue to work on projects and exercises to get comfortable with DOM Manipulation!

flw1-u1l6-23-24-student-exercises's People

Contributors

cn-curriculum avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.