JavaScript and DOM Manipulation Quiz
21 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which method is used to create a new element in the DOM?

  • document.addElement()
  • document.createElement() (correct)
  • document.insertElement()
  • document.newElement()
  • What does the method elem.getAttribute(name) return?

  • The list of all attributes of the element
  • True or false indicating if the attribute exists
  • The default value of the attribute
  • The value of the specified attribute (correct)
  • How can you modify the color style of an element using JavaScript?

  • elem.css('color', 'green');
  • elem.changeStyle('color', 'green');
  • elem.style.color = 'green'; (correct)
  • document.setStyle(elem, 'color', 'green');
  • Which method is used to remove an attribute from a DOM element?

    <p>elem.removeAttribute(name)</p> Signup and view all the answers

    What is a potential drawback of using innerHTML to change content?

    <p>It can introduce vulnerabilities by allowing rogue code.</p> Signup and view all the answers

    What method is used to remove an event handler added with addEventListener?

    <p>removeEventListener()</p> Signup and view all the answers

    Which of the following is NOT an intrinsic event attribute in HTML?

    <p>ondrop</p> Signup and view all the answers

    Which statement best describes the role of JavaScript in web development?

    <p>JavaScript enables dynamic behavior in web content.</p> Signup and view all the answers

    How can you include an external JavaScript file in your HTML?

    &lt;script src='file.js'>&lt;/script> Signup and view all the answers

    Which method is used to trigger a function when an HTML document has loaded?

    <p>addEventListener('load', function)</p> Signup and view all the answers

    What type of JavaScript object does 'null' represent?

    <p>Primitive type</p> Signup and view all the answers

    Which of the following is not a type of DOM selection method?

    <p>selectElement()</p> Signup and view all the answers

    What happens when an event handler is executed in JavaScript?

    <p>It runs logic in response to a specific event.</p> Signup and view all the answers

    Which method is used to add a child node to a parent node in the DOM?

    <p>parent.appendChild(childNode)</p> Signup and view all the answers

    What is the purpose of the preventDefault() method in event handling?

    <p>To prevent the default action of the event from occurring</p> Signup and view all the answers

    Which of the following is NOT a phase of DOM event flow?

    <p>Event captivation phase</p> Signup and view all the answers

    How can you replace one node with another in the DOM?

    <p>parentNode.replaceChild(newChild, oldNode)</p> Signup and view all the answers

    Which event would you use to detect when a key is pressed down?

    <p>keydown</p> Signup and view all the answers

    What does the addEventListener() function do?

    <p>Binds an event handler to an event for an element</p> Signup and view all the answers

    Which property is used to assign an event handler to an element using Node Properties?

    <p>onclick</p> Signup and view all the answers

    To remove a child node from a parent node, which method is used?

    <p>parent.removeChild(childNode)</p> 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
    • 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 or false
    • 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 or objectName["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.

    Quiz Team

    Related Documents

    JavaScript Implementations PDF

    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.

    More Like This

    DOM Manipulation in JavaScript
    18 questions
    DOM Manipulation Methods
    9 questions

    DOM Manipulation Methods

    ResoundingLaboradite9376 avatar
    ResoundingLaboradite9376
    Week 13_Lesson 1
    76 questions

    Week 13_Lesson 1

    BetterThanExpectedLearning9144 avatar
    BetterThanExpectedLearning9144
    Week 13
    57 questions

    Week 13

    BetterThanExpectedLearning9144 avatar
    BetterThanExpectedLearning9144
    Use Quizgecko on...
    Browser
    Browser