CS133JS Beginning Programming: JavaScript

The Document Object Model

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Page I/O
4. Repetition: while, do while, and for9. Dynamic Web Pages
5. Midterm Check-Point10. Review for final quiz

Table of Contents

Introduction

Announcements

For Winter 2024

Q and A

 

Web Page Input and Output

A brief introduction

What is the DOM?

The browser has a set of built-in JavaScript objects that represent the web page and all its HTML elements. The objects are arranged in a hierarchy:

DOM-model

By Birger Eriksson - Own work, CC BY-SA 3.0, Link

 

HTML for the web page in the DOM diagram above

 

Referencing HTML Elements

We can reference elements directly through the DOM tree, like this:

But usually, we use some method to search for an element by id, selector or some other identifying feature.

For the examples below, assume we have this in our web page:

By id attribute

By CSS selector

Using querySelector with a single selector

Review of CSS

in a CSS rule, the first part is the selector. For example, in the rule below, .cit is the class selector:

 

Back to the querySelector

In JavaScript, we can use document.querySelector to get a reference to an HTML element.

Using querySelector with multiple selectors

For the html below,

In the JavaScript example below, we are using the selectors being passed to the querySelector method to put text into the spans.

 

I/O Using HTML Elements

In previous examples and lab assignments you've used the prompt function to get input document.write, or getElementById with innerHTML to put output on a web page.

Now we'll look at a couple new ways to do input and output (I/O).

textContent

This is another way to access the content between tags. The difference between this property and innerHTML is that textContent just gets or set text, no HTML tags will be processed and no formatting done on the web page. Here's an example showing the differences:

Demo of innerHTML vs. textConent

The textContent property is the best one to use when you just need to output text (without HTML tags) to a web page.

value

This object property is useful for getting or setting the value in an HTML <input> element. The input element would normally be in an HTML form.

 

JavaScript Events

JavaScript can respond to various actions a user preforms when interacting with a web page in a browser. These user actions are called events.

Common events

Event Handlers

Event handlers are JavaScript functions that are called when events occur.

Using a Button to Get Input

We can use a button with an onclick event handler to get user input. Here's an example:

Note: The onclick event can be used with any element, but we frequently use it with a button.

When to Use an HTML <form>

The purpose of a <form> is to send the information entered in the form to a program running on a server. Only use a form if that is what you want to do. If you are just using input elements with JavaScript running in the browser, don't put the input elements in a form.

When you click a button on a form, the information entered on the form will be sent in an HTTP request either to a program at a URL specified in the form's action attribute or back to the URL of the web page the form is inthis is called a postback. A postback will reset the web page to its original state and any information entered on the form will be lost.

 

Further Reading

Tutorials

Reference

DOM (Document Object Model) - MDN

Event Reference - MDN

CSS Selectors - MDN

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, 2020, revised , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.