Podcast
Questions and Answers
Which method is used to create a new element in the DOM?
Which method is used to create a new element in the DOM?
What does the method elem.getAttribute(name) return?
What does the method elem.getAttribute(name) return?
How can you modify the color style of an element using JavaScript?
How can you modify the color style of an element using JavaScript?
Which method is used to remove an attribute from a DOM element?
Which method is used to remove an attribute from a DOM element?
Signup and view all the answers
What is a potential drawback of using innerHTML to change content?
What is a potential drawback of using innerHTML to change content?
Signup and view all the answers
What method is used to remove an event handler added with addEventListener?
What method is used to remove an event handler added with addEventListener?
Signup and view all the answers
Which of the following is NOT an intrinsic event attribute in HTML?
Which of the following is NOT an intrinsic event attribute in HTML?
Signup and view all the answers
Which statement best describes the role of JavaScript in web development?
Which statement best describes the role of JavaScript in web development?
Signup and view all the answers
How can you include an external JavaScript file in your HTML?
How can you include an external JavaScript file in your HTML?
Signup and view all the answers
Which method is used to trigger a function when an HTML document has loaded?
Which method is used to trigger a function when an HTML document has loaded?
Signup and view all the answers
What type of JavaScript object does 'null' represent?
What type of JavaScript object does 'null' represent?
Signup and view all the answers
Which of the following is not a type of DOM selection method?
Which of the following is not a type of DOM selection method?
Signup and view all the answers
What happens when an event handler is executed in JavaScript?
What happens when an event handler is executed in JavaScript?
Signup and view all the answers
Which method is used to add a child node to a parent node in the DOM?
Which method is used to add a child node to a parent node in the DOM?
Signup and view all the answers
What is the purpose of the preventDefault() method in event handling?
What is the purpose of the preventDefault() method in event handling?
Signup and view all the answers
Which of the following is NOT a phase of DOM event flow?
Which of the following is NOT a phase of DOM event flow?
Signup and view all the answers
How can you replace one node with another in the DOM?
How can you replace one node with another in the DOM?
Signup and view all the answers
Which event would you use to detect when a key is pressed down?
Which event would you use to detect when a key is pressed down?
Signup and view all the answers
What does the addEventListener() function do?
What does the addEventListener() function do?
Signup and view all the answers
Which property is used to assign an event handler to an element using Node Properties?
Which property is used to assign an event handler to an element using Node Properties?
Signup and view all the answers
To remove a child node from a parent node, which method is used?
To remove a child node from a parent node, which method is used?
Signup and view all the answers
Study Notes
JavaScript Implementations
- JavaScript core comprises three components: ECMAScript, DOM, and BOM
- ECMAScript provides the language's core syntax and semantics
- DOM (Document Object Model) represents the structure of HTML/XML documents as nodes
- BOM (Browser Object Model) provides a programming interface for interacting with the browser environment
Window Object
- Properties of the Window object are globally accessible through browser APIs
- Properties vary across different browsers due to vendor implementations
- innerWidth and innerHeight: size of the viewport (minus borders, toolbars)
- outerWidth and outerHeight: size of the entire browser window
- resizeTo(x,y) and resizeBy(x,y): method to resize the browser window
- window.open(URL, "newFrame"): methods for navigating to a specific URL
Location Object
- Provides information about the current document and allows navigation
- The
location
object has properties for describing the current document's URL - Properties include hash, host, hostname, href, pathname, port, protocol, search, username, password, and origin
Navigator Object
- Provides information about the user's browser and system environment
- Properties include: battery, connection, cookieEnabled, credentials, deviceMemory, doNotTrack, geolocation, getVRDisplays(), getUserMedia(), languages, locks, mediaCapabilities, mediaDevices, maxTouchPoints, online, oscpu, permissions, platform, plugins, registerProtocolHandler, requestMediaKeySystemAccess, and sendBeacon()
Screen Object
- Contains properties related to the screen's display dimensions and layout
- Properties include: availHeight, availLeft, availTop, availableWidth, colorDepth, height, left, pixelDepth, top, width, and orientation
DOM (Document Object Model)
- A structured representation of the HTML structure (representing a web page as a tree)
- The root DOM element is the document object
- Each element is a node, with attributes (properties)
- The DOM can be modified dynamically with JavaScript
Retrieving DOM Nodes
- Nested objects/elements accessed via dot notation
- Document.images[index]: access a particular image in a document
- document.getElementsByTagName(tagname): returns an array of all elements with the specified tag name (including non-matched elements such as paragraphs)
- document.getElementById(id): returns the element with the corresponding id attribute
- document.getElementsByClassName(classname): Returns an array of all elements with the given class names
- document.querySelector/querySelectorAll: Selects the element according to the CSS Selector
Accessing DOM Node Attributes
- elem.AttributeName: individual attribute name/value
- elem.attributes: attribute collection
- elem.hasAttribute(name): checks attribute availability
- elem.getAttribute(name): gets attribute value
- elem.setAttribute(name, value): sets attribute value
- elem.removeAttribute(name): removes attribute
- elem.style.CSSAttributeName: access CSS attributes for a node
- elem.className: class string name
- elem.classList: class list (add/remove/toggle methods)
Accessing DOM Node' Attributes - Examples
- getAttribute(“attribute name”) -> retrieves the attribute value
- setAttribute(“attribute name”,”new value”): Changes the attribute value
Adding and Removing Elements
- document.createElement(tagName): creates a new element
- parentNode.appendChild(childNode): adds the node to the end of the parent node
- parentNode.insertBefore(InsertedNode,Node2): inserts element before another element
- parentNode.replaceChild(newChild, oldChild): replaces a node with another
DOM Events & Event Flow
- DOM Event Flow: capturing, target, bubbling
- Event handler (event listener): Function called in response to an event
- Event handlers often begin with the “on” prefix
- Events can be triggered by user interaction or browser actions (e.g., loading a page)
DOM Events Examples
- event Object: target, type, preventDefault() for controlling event actions
- Keyboard events: key, keyCode, altKey, ctrlKey, shiftKey
- Mouse events: pageX, pageY, button, altKey, ctrlKey, shiftKey
DOM Event Handlers
- Assigning event listeners via addEventListener() and removeEventListener()
Intrinsic Events in Web App
- Event attributes (onload, onclick, onmouseover, onfocus, onkeyup, onsubmit, onselect): built-in events
- Specific attributes are used to bind events to HTML elements
JavaScript Language - The Basics
- HTML/CSS only allow for static content and do not allow dynamic interaction
- JavaScript is responsible for performing the dynamic behavior of web pages
- JavaScript interacts with HTML/CSS content by accessing elements, manipulating their attributes, or performing actions
- JavaScript interacts with data from remote HTTP web servers
JavaScript Primitive Types
- number, string, boolean, null, undefined
- Primitive data types in JavaScript have no methods or behaviors and can't be modified without creating a new type.
JavaScript Variables & Constants
- Variable names can use letters, numbers, underscore, and dollar sign. However, it has to begin with a letter or underscore
- Local variables are declared with the
var
keyword in block scope -
const
declares a constant variable
JavaScript Strings
- Strings are series of Unicode characters
- Strings in JavaScript are immutable; manipulations create a new string (with concat) instead of modifying the original
- Methods include
indexOf()
,substring()
, and others
JavaScript Booleans
- Boolean values in JavaScript can be
true
orfalse
- Falsy values:
0
,NaN
,""
,null
,undefined
,false
- Truthy Values are all other values other than falsy values
JavaScript Null & Undefined
-
null
designates an intentional absence of value (an object) -
undefined
indicates that a variable is declared but isn't assigned a value.
JavaScript Arrays
- Arrays represent ordered collections of values
- Arrays are indexed starting from 0
- Array methods for adding/removing elements
JavaScript Objects
- Objects store key-value pairs
- Values are accessed via
objectName.propertyName
orobjectName["propertyName"]
.
JavaScript Error Objects
- Error objects are created when an exception occurs in the JavaScript program
- Error objects have properties (attributes): Message (description) and Name (error type)
JavaScript Conditionals & Loops
- Conditional statements (
if
,else if
,else
) control the execution flow based on conditions - Loop structures (
for
,while
,do...while
) repeat sets of expressions
JavaScript Operators & Expressions
- Operators perform operations on operands (e.g., arithmetic, comparison, logical)
- Special operators exist for checking whether types and values are equal using loose or strict comparisons such as the
==
and===
.
Comparing Objects
- Objects are only equal if they are the same object (
a===b
)
Declaring and Using Functions
- Functions perform specific tasks in JavaScript
- Functions in JavaScript are declared using
function
keyword, optionally including parameters.
Creating Custom Objects
- Use the
new
keyword to create an object instance of a function.
Prototypes
- JavaScript objects inherit properties and methods from their prototypes
- Prototypes are blueprints for creating objects
- Properties and methods of a prototype can be extended without modification affecting other instances
Debugging JavaScript
- Write error messages to the browser's development tools console
- Use addXToThePage() for debugging directly on the web page
Asynchronous JavaScript
- JavaScript is not truly asynchronous
- JavaScript programs in a browser environment perform tasks asynchronously using event loop
Asynchronous JavaScript with Callbacks
- Functional programming paradigm
- Functions are passed as arguments to other functions (callbacks)
- Callbacks are executed when a condition is met or an asynchronous event occurs
- Events, timers, and network interactions are examples of asynchronous operations
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Related Documents
Description
Test your knowledge on JavaScript methods related to DOM manipulation with this quiz. Explore topics such as creating elements, modifying styles, and event handling. Enhance your understanding of how JavaScript interacts with HTML and the Document Object Model.