Podcast
Questions and Answers
What is the top-level object in the DOM hierarchy?
What is the top-level object in the DOM hierarchy?
- document
- body
- html
- window (correct)
What does the HTML DOM allow JavaScript to do?
What does the HTML DOM allow JavaScript to do?
- Access and change HTML elements (correct)
- Only access HTML elements
- Only change HTML elements
- Bypass HTML elements
What property of the window.screen
object gives the total screen width?
What property of the window.screen
object gives the total screen width?
- availWidth
- height
- availHeight
- width (correct)
Which property specifies the bit value indicating color range?
Which property specifies the bit value indicating color range?
Which of the following methods displays a simple alert box with a message and an "OK" button?
Which of the following methods displays a simple alert box with a message and an "OK" button?
Which method displays a confirmation box with 'OK' and 'Cancel' buttons?
Which method displays a confirmation box with 'OK' and 'Cancel' buttons?
Which method displays a prompt box with a text input field?
Which method displays a prompt box with a text input field?
Which object is a child of the window
object?
Which object is a child of the window
object?
How do you access the page title using the document
object?
How do you access the page title using the document
object?
What does document.URL
provide?
What does document.URL
provide?
Which property returns the domain name of the document?
Which property returns the domain name of the document?
How can you return the number of images in a document?
How can you return the number of images in a document?
What is returned when accessing elements by tag name?
What is returned when accessing elements by tag name?
Which method retrieves a single element by its ID?
Which method retrieves a single element by its ID?
Which method returns all elements with a specific class name?
Which method returns all elements with a specific class name?
What does the innerHTML
property allow you to do?
What does the innerHTML
property allow you to do?
What happens when document.write()
is executed after the page has loaded?
What happens when document.write()
is executed after the page has loaded?
Which method creates a new HTML element dynamically?
Which method creates a new HTML element dynamically?
Which of the following methods would you use to add a newly created element to the page?
Which of the following methods would you use to add a newly created element to the page?
Which method is used to dynamically add or modify attributes of an HTML?
Which method is used to dynamically add or modify attributes of an HTML?
What is an 'event-handler'?
What is an 'event-handler'?
Which event fires when the page has completely loaded into the browser?
Which event fires when the page has completely loaded into the browser?
What is a mouseover event?
What is a mouseover event?
What does the event object contain?
What does the event object contain?
What Javascript functionality allows the selection of multiple options?
What Javascript functionality allows the selection of multiple options?
What JavaScript functionality allows the selection of only one options?
What JavaScript functionality allows the selection of only one options?
The .checked
property is what type?
The .checked
property is what type?
What does the selectedIndex
property of a select element contain?
What does the selectedIndex
property of a select element contain?
Which tag is used to create the dropdown list?
Which tag is used to create the dropdown list?
What is a DOM?
What is a DOM?
With the HTML DOM, Javascript can access and change
With the HTML DOM, Javascript can access and change
Javascript can react to existing HTML events except:
Javascript can react to existing HTML events except:
Fill in the blank: The top-level DOM object has a _____ child object.
Fill in the blank: The top-level DOM object has a _____ child object.
To access the first image in the document body, specified by an HTML tag, is represented by which of the following?
To access the first image in the document body, specified by an HTML tag, is represented by which of the following?
Elements can be accessed via Index in Component arrays using the methods bellow, excpet:
Elements can be accessed via Index in Component arrays using the methods bellow, excpet:
To change text (No HTML), the method used to change the text in the DOM is
To change text (No HTML), the method used to change the text in the DOM is
In Javascript, the recommended way to executre a function, specific to an element?
In Javascript, the recommended way to executre a function, specific to an element?
What is the hierarchical tree structure that the browser uses to organize web page components called?
What is the hierarchical tree structure that the browser uses to organize web page components called?
Which property provides the available width of the screen, excluding system UI elements?
Which property provides the available width of the screen, excluding system UI elements?
What does the 8-bit
color depth specify?
What does the 8-bit
color depth specify?
What does the document.domain
property retrieve?
What does the document.domain
property retrieve?
What is the primary purpose of document.createElement()
?
What is the primary purpose of document.createElement()
?
Flashcards
Document Object Model (DOM)
Document Object Model (DOM)
A hierarchical tree organizing web page components. Each component relies under the top-level window object.
Screen object
Screen object
A child object of the DOM presenting the user's monitor resolution in pixels.
Window.screen.width
Window.screen.width
Total screen width in pixels.
Window.screen.height
Window.screen.height
Signup and view all the flashcards
Window.screen.availWidth
Window.screen.availWidth
Signup and view all the flashcards
Window.screen.availHeight
Window.screen.availHeight
Signup and view all the flashcards
Window.screen.colorDepth
Window.screen.colorDepth
Signup and view all the flashcards
window.alert(message)
window.alert(message)
Signup and view all the flashcards
window.confirm(message)
window.confirm(message)
Signup and view all the flashcards
window.prompt(message, defaultValue)
window.prompt(message, defaultValue)
Signup and view all the flashcards
Document Object
Document Object
Signup and view all the flashcards
document.title
document.title
Signup and view all the flashcards
document.URL
document.URL
Signup and view all the flashcards
document.domain
document.domain
Signup and view all the flashcards
document.forms.length
document.forms.length
Signup and view all the flashcards
document.images.length
document.images.length
Signup and view all the flashcards
document.links.length
document.links.length
Signup and view all the flashcards
document.scripts.length
document.scripts.length
Signup and view all the flashcards
document.images[0]
document.images[0]
Signup and view all the flashcards
document.getElementById()
document.getElementById()
Signup and view all the flashcards
document.getElementsByTagName()
document.getElementsByTagName()
Signup and view all the flashcards
document.getElementsByClassName()
document.getElementsByClassName()
Signup and view all the flashcards
innerHTML
innerHTML
Signup and view all the flashcards
innerText
innerText
Signup and view all the flashcards
document.write()
document.write()
Signup and view all the flashcards
createElement()
createElement()
Signup and view all the flashcards
appendChild()
appendChild()
Signup and view all the flashcards
setAttribute()
setAttribute()
Signup and view all the flashcards
Event Handlers
Event Handlers
Signup and view all the flashcards
load event
load event
Signup and view all the flashcards
Handling events
Handling events
Signup and view all the flashcards
Mouse Events
Mouse Events
Signup and view all the flashcards
Click event
Click event
Signup and view all the flashcards
Dblclick Event
Dblclick Event
Signup and view all the flashcards
Mouseover Event
Mouseover Event
Signup and view all the flashcards
Mouseout Event
Mouseout Event
Signup and view all the flashcards
Event Object
Event Object
Signup and view all the flashcards
Checkboxes
Checkboxes
Signup and view all the flashcards
Radio Buttons
Radio Buttons
Signup and view all the flashcards
Grouping by name
Grouping by name
Signup and view all the flashcards
.checked Property
.checked Property
Signup and view all the flashcards
selectedIndex
selectedIndex
Signup and view all the flashcards
value
value
Signup and view all the flashcards
options
options
Signup and view all the flashcards
length
length
Signup and view all the flashcards
multiple
multiple
Signup and view all the flashcards
Study Notes
Meet DOM
-
The browser organizes web page components into a hierarchical tree, this is known as the Document Object Model (DOM)
-
Each component is nested under the top-level window object
-
With the HTML DOM, JavaScript can:
- Access all HTML elements
- Change all HTML elements
-
The DOM enables dynamic webpage changes without reloading
-
DOM helps JavaScript interact with HTML and CSS
-
It's essential for making interactive web applications
Inspect Properties
- The top-level DOM window object has a screen child object
- The screen object has properties describing the user's monitor resolution
- Resolutions are in pixels
Screen Dimensions and Usable Space
- Total screen size is accessed via:
Window.screen.width
: Total screen widthWindow.screen.height
: Total screen height
- Usable screen space is found using:
Window.screen.availWidth
: Available widthWindow.screen.availHeight
: Available height
- These properties optimize layout design, ensuring content fits within the user's available screen area
Color Depth Information
Window.screen.colorDepth
is the bit value indicating color range- The common values are:
- 8-bit: 256 colors (Low Color)
- 16-bit: 65,536 colors (High Color)
- 24-bit: Millions of colors (True Color)
- 32-bit: Billions of colors (Deep Color)
Show Dialogs
- The top-level DOM window object uses three methods to display dialog messages to the user:
- Displaying a simple alert box with a message and an "OK" button using
window.alert(message)
- Displaying a confirmation box with "OK" and "Cancel" buttons,
window.confirm(message)
- Returns true for "OK" and false for "Cancel"
- Displaying a prompt box with a text input field
window.prompt(message, defaultValue)
- An "OK" button returns the user input value
- A "Cancel" button returns null
- Displaying a simple alert box with a message and an "OK" button using
Extract Info
- The document object is a child of the window object
- Document object provides access to and manipulation of the HTML document
- It's used widely in JavaScript for dynamic page interactions
- It also offers access to:
- Page title
(document.title)
- Current URL
(document.URL)
- Domain name
(document.domain)
- Last modified date
(document.lastModified)
- Referrer URL
(document.referrer)
- Page title
- Changing the document title:
document.title = "New title"
- The document object is useful for dynamically updating web pages
Address Arrays
- The document object has child objects like forms, images, links, styleSheets, and scripts
- Each object that is represented as an array corresponds to elements in the HTML document
- The number of objects in the document:
- Forms:
document.forms.length;
- images:
document.images.length;
- links:
document.links.length;
- scripts:
document.scripts.length;
- Forms:
Address Array
- The first image in the document body specified by an HTML tag:
document.images[0];
- Its URL can be referenced using:
document.images[0].src;
- Assigning a new URL dynamically replaces the old image:
document.images[0].src = 'new URL';
Address Elements
- Modern methods provides three efficient ways to access elements:
- Accessing element by ID
- Accessing element by tag name
- Accessing element by class name
- Error prone method, accessing elements in component arrays via index
- Modern Methods with JavaScript:
- Accessing element by ID
- Retrieves a single element by its id:
document.getElementById("intro").style.color = "blue";
- Best for unique elements like headers, buttons, or sections
- Retrieves a single element by its id:
- Accessing element by ID
Accessing Element by Tag Name
- Tag name is the method for retrieving "all" elements with the given tag name
- It returns an HTMLCollection as an array-like structure
- Example:
document.getElementsByTagName("p")[0].style.fontSize = "18px";
- Example:
- This is used for changing properties for multiple elements of the same type
Accessing Element by Class Name
- Class name is the method for retrieving "all" elements with a specific class
- It returns an HTMLCollection
Example
document.getElementsByClassName("highlight")[0].style.backgroundColor ="yellow";
- Used for applying styles or modifications to multiple elements at once
Write Content
- The DOM uses three methods to write content:
- Insert or Change HTML Content (innerHTML)
- Insert or Change Text (No HTML)
- Writing Content Directly to the Page (document.write)
Insert or Change HTML Content (innerHTML)
- HTML content allows setting or retrieving an element's HTML content
- It supports text and HTML elements
document.getElementById("output").innerHTML = "<b>Welcome to JavaScript!</b>";
- Output = Welcome to JavaScript!
Insert or Change Text (No HTML)
- The innerText property of an element returns "only the content" between its opening and closing tags
- It sets or retrieves "only text content", ignoring HTML tags and treating them as plain text
document.getElementById("output").innerText = "<b>Welcome to JavaScript!</b>";
- Output = Welcome to JavaScript! (renders as plain text).
Writing Content Directly to the Page (document.write)
- Content is written directly into the document body
- Using this method replaces all existing HTML content if executed after page load
- document.write is not recommended for modern web development
- It's mainly used for testing or when loading a new page dynamically
document.write(<b>Welcome to JavaScript!</b>");
Creating an HTML Element Dynamically
createElement(tagName)
creates an HTML element dynamically- It takes the
tag name
as an argument - Content is added using either:
.innerHTML
or.innerText
let newHeading = document.createElement("h2");
newHeading.innerText = "Hello, JavaScript!";
Adding the New Element to the Page
- The new element appends inside an existing parent
- It is placed at the end of the parent's content
let container = document.getElementById("content");
let newParagraph= document.createElement("p");
newParagraph.innerText = "Hello, JavaScript!"
container.appendChild(newParagraph);
Adding Attributes to Elements
- JavaScript adds or modifies HTML element attributes for flexibility in manipulating the DOM
- Setting a new attribute for an element requires two arguments:
- The attribute name
- The attribute value
let image = document.createElement("img");
image.setAttribute("src","image.jpg");
image.setAttribute("alt", "Dynamic Image");
document.body.appendChild(newParagraph);
Events
- The DOM allows JavaScript to react to "events" providing functions that execute when an event happens
- Known as "event-handlers", functions can react to events such as:
- load – browser loads page
- click - mouse button clicks
- keydown - a key is pressed
- change – input field edits
- submit – user submits HTML form
Event Process
- To work with events in JavaScript, consider:
- The Event: What happens that you want to respond to
- Load
- Click
- Mousemove
- Keydown, ketup
- Submit
- The Reaction: The function you define to react to
- Change style (e.g., button red)
- Add/remove text/content
- Send data to server
- Show/hide an element
- The Event: What happens that you want to respond to
Handling Events
- Two main ways to handle events:
- HTML Event Attributes (Inline Method)
- JavaScript (addEventListener Method)
HTML Event Attributes (Inline Method)
- Directly add the event in the HTML element using an attribute:
- onclick
- onmouseover
- Example:
<button onclick="myFunction()">Click Me!</button>
function myFunction() {
alert ("Hello World!");
}
JavaScript (addEventListener Method)
- Registers a function to be executed when a specific event occurs on a specific element
- This is the recommended way to handle events
- Example:
element.addEventListener("click", myFunction);
function myFunction() {
alert ("Hello World!");
}
Load Events
- The load event is fired when the entire page has completely loaded
- In other words, the browser waits for everything to finish loading before triggering the event.
- To respond to the load event, there are two main ways:
- Using the window.onload Property
- Using the add event listener
Mouse Events
- Mouse events are triggered when the user interacts with the mouse over an element on the page.
- Common Mouse Events are:
- "Click" is triggered by clicking elements using the mouse
- "Dblclick" is triggered by double clicking elements
- "Mouseover" is triggered by the mouse entering inside the element
- "Mouseout" is triggered by mouse leaving the element
Event Values
- The event object contains event information, such as:
- What type of event it was (e.g. click, keydown)
- Where it happened (mouse coordinates)
- Which key was pressed
- Which element triggered the event
element.addEventListener("click", myFunction(event));
function myFunction(event) {
alert ("Event type:", event.type)
}
Checkboxes and Radio Buttons
- Checkboxes allow users to select multiple options independently
- Radio Buttons allow users to select only one option from a group
- All buttons in a radio button group share the same name attribute
Grouping Behavior and JS Access
- By name, both checkboxes and radio buttons are grouped
- In JavaScript, access the group with:
let toppings = document.getElementsByName("Top");
let sizes = document.getElementsByName("Size");
- Result is an array-like object, enabling:
- Looping
- Accessing elements using an index
- Reading/assigning the .checked property
Using .checked to Control Selection
- The
.checked
Property is a Boolean property on both checkboxes and radio buttons- "true" indicates selected, "false" indicates not selected
for (let i = 0; i < toppings.length; i++) { if (toppings[i].checked) { console.log(toppings[i].value); } }
### Select Options
- The `<select>` element creates an options dropdown list
- Javascript can interact with a `<select>` element using properties:
- `selectedIndex` - Index of the selected option (starts at 0)
- `value` - Value of the selected option
- `options` - Collection of all `<option>` elements inside `<select>`
- `length` - Number of options in the dropdown
- `multiple` - Boolean that indicates whether multiple options can be selected
### Summary
- The document object consists of `title`, `URL`, `domain`, `lastModified`, and `referrer` properties which describe the document
- The document object consists of `forms`, `images`, `links`, `styleSheets`, and `scripts` child objects which are arrays of document components
- The document object consists of `getElementById()`, `getElementsByTagName()`, and `getElementsByClassName()` methods to reference HTML elements
- The `innerHTML` and `innerText` properties that are part of the document object can be used to write content into existing elements
- The document object consists of `createElement()`, `appendChild()`, and `setAttribute()` methods that can add content to a document
- The DOM handles javascript reactions to user actions for events such as `load`, `click`, `keydown`, `change`, and `submit`
- Event-handler functions can be assigned to an object property or specified by the `addEventListener()` method
- Event objects can be passed to an event-handler function, the event determined by the `event.type` property
- Radio and checkbox button objects consist of checked boolean properties set to true when the button is selected.
- Selection list objects consist of a selectedIndex property that contains the index number of currently selected options array element
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.