101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    JavaScript 101

    Receive aemail containing the next unit.
    • Introduction to JavaScript
      • 1.1What is JavaScript?
      • 1.2Setting up your environment
      • 1.3Basic syntax and variables
    • Data Types and Variables
      • 2.1Understanding data types
      • 2.2Variables and constants
      • 2.3Operators
    • Control Structures
      • 3.1Conditional statements
      • 3.2Loops
      • 3.3Error handling
    • Functions
      • 4.1Defining functions
      • 4.2Function expressions
      • 4.3Arrow functions
    • Objects and Arrays
      • 5.1Understanding objects
      • 5.2Understanding arrays
      • 5.3Array methods
    • Document Object Model (DOM)
      • 6.1What is the DOM?
      • 6.2Selecting and manipulating elements
      • 6.3Event handling
    • Asynchronous JavaScript
      • 7.1Callbacks
      • 7.2Promises
      • 7.3Async/Await
    • API Interaction
      • 8.1What is an API?
      • 8.2Fetch API
      • 8.3Working with JSON
    • Debugging
      • 9.1Console methods
      • 9.2Debugging tools
      • 9.3Common JavaScript errors
    • JavaScript Libraries
      • 10.1Introduction to libraries
      • 10.2Using jQuery
      • 10.3Using lodash
    • JavaScript Frameworks
      • 11.1Introduction to frameworks
      • 11.2Using React
      • 11.3Using Vue.js
    • Testing and Deployment
      • 12.1Writing tests with Jest
      • 12.2Deployment with Netlify
      • 12.3Continuous integration
    • Final Project
      • 13.1Project proposal
      • 13.2Project development
      • 13.3Project presentation

    Document Object Model (DOM)

    Selecting and Manipulating Elements in the DOM

    convention for representing and interacting with objects in HTML, XHTML and XML documents

    Convention for representing and interacting with objects in HTML, XHTML and XML documents.

    The Document Object Model (DOM) is a crucial aspect of web development, as it allows us to interact with the webpage in real-time. One of the most common tasks when working with the DOM is selecting and manipulating elements. This article will guide you through the process of selecting elements using different methods, understanding NodeList and HTMLCollection, changing element content, manipulating element attributes, and creating, inserting, and deleting elements.

    Selecting Elements

    There are several ways to select elements in the DOM:

    • getElementById: This method allows you to select an element by its id attribute. For example, document.getElementById('myId') would select the element with the id of 'myId'.

    • querySelector: This method allows you to select an element using any CSS selector. For example, document.querySelector('.myClass') would select the first element with the class of 'myClass'.

    • querySelectorAll: Similar to querySelector, but instead of returning the first match, it returns all matches as a NodeList.

    • getElementsByClassName and getElementsByTagName: These methods return a live HTMLCollection of elements with the specified class name or tag name.

    Understanding NodeList and HTMLCollection

    When you select multiple elements (like with querySelectorAll, getElementsByClassName, or getElementsByTagName), you get a NodeList or an HTMLCollection. These are array-like objects that contain a list of nodes. You can loop over them using a for loop or forEach (only for NodeList).

    Changing Element Content

    You can change the content of an element using the innerHTML and textContent properties. innerHTML returns or sets the HTML content (inner HTML) of an element. textContent returns or sets the text content of a node and its descendants.

    Manipulating Element Attributes

    You can get, set, and remove attributes (like id, class, src, etc.) using the getAttribute, setAttribute, and removeAttribute methods.

    Creating, Inserting, and Deleting Elements

    You can create new elements using the createElement method, and then add them to the DOM using appendChild or insertBefore. To remove an element, you can use the removeChild method.

    In conclusion, understanding how to select and manipulate elements in the DOM is a fundamental skill in JavaScript programming. It allows you to create dynamic and interactive webpages, where content can be changed, added, or removed in response to user actions.

    Test me
    Practical exercise
    Further reading

    My dude, any questions for me?

    Sign in to chat
    Next up: Event handling