Skip to content

codeadamca/javascript-dom

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A Basic Introduction to JavaScript and the DOM

When a browser renders a webpage from an HTML document, the browser creats a DOM (Document Object Model). This DOM can be use by JavaScript to add and/or manipulate elements in the document.

Example DOM

For example, the following HTML:

<!doctype html>
<html>
<head>
    <title>The JavaScript DOM</title>
</head>
<body>

    <h1>The JavaScript DOM</h1>

    <p>Mauris convallis dictum odio. Quisque euismod finibus.</p>

    <a href="https://codeadam.ca">codeadam.ca</a>
    
</body>
</html>

Would have the following DOM:

DOM Chart

Referencing Elements

The easiest method of referencing an element in the DOM is by giving the elements IDs:

<!doctype html>
<html>
<head>
    <title>The JavaScript DOM</title>
</head>
<body>

    <h1 id="heading">The JavaScript DOM</h1>

    <p id="paragraph">Mauris convallis dictum odio. Quisque euismod finibus.</p>

    <a id="link" href="https://codeadam.ca">codeadam.ca</a>
    
</body>
</html>

To reference the heading we would use the document.getElementById('heading'). We can then use a DOM element to change the properties of the element. For example, to change the heading to red we would use the following JavaScript:

document.getElementById('heading').style.color = "red";

To change the href value of the link we would use the following JavaScript:

document.getElementById('link').href = "https://codeadam.ca/learning/javascript-dom.html";

Trying It Out

Create a new HTML document, add a heading, some images, and some paragraphs. Use Lipsum for some quick paragraph content. Add some JavaScript to the HTML document that will complete the following objectives:

  1. Change the text colour of the h1 element.
  2. Change the text of the first p element.
  3. Change the border colour of one of the img elements.
  4. Change the background colour of the body element.

Full tutorial URL:
https://codeadam.ca/learning/javascript-dom.html


Repo Resources


About

A basic example of using JavaScript to make changes to a webpage using the DOM.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages