Skip to main content
April 1, 2026Dan Rodney/7 min read

Targeting HTML Elements

Master JavaScript DOM Manipulation with Chrome DevTools

Core Skills You'll Master

Chrome DevTools Mastery

Learn to inspect, debug, and experiment with HTML elements using professional developer tools. Essential for any web developer's toolkit.

DOM Element Selection

Master the getElementById() method to precisely target and manipulate specific HTML elements in your web pages.

Dynamic Property Control

Gain the ability to get and set element properties, values, and styles programmatically using JavaScript.

Topics Covered in This JavaScript & jQuery Tutorial:

Master Chrome's DevTools for debugging, select HTML elements with getElementById(), manipulate DOM elements dynamically, and control element properties and values programmatically.

Exercise Preview

ex prev target html

What You'll Build

This hands-on exercise teaches JavaScript DOM manipulation through Chrome's DevTools Console. You'll learn to select, modify, and control HTML elements dynamically without writing permanent code.

Exercise Overview

In this hands-on exercise, we'll demystify JavaScript's DOM manipulation capabilities by diving into the browser's inner workings. Using Chrome's industry-standard DevTools, we'll explore the HTML document structure, select specific elements, and modify them in real-time using JavaScript. This foundational skill is essential for any developer working with interactive web applications, as it demonstrates the core principles behind dynamic user interfaces and data manipulation.

Getting Started

We'll begin by setting up our development environment and familiarizing ourselves with Chrome's powerful debugging tools—skills that remain fundamental to web development in 2026.

  1. On your Desktop, navigate into Class Files > yourname-JavaScript jQuery Class > Form-Fields folder.
  2. We want to use Chrome's comprehensive Developer Tools (DevTools), so we'll open our page in Chrome. CTRL–click (Mac) or Right–click (Windows) on form.html and choose Open With > Google Chrome.
  3. CTRL–click (Mac) or Right–click (Windows) anywhere on the page and choose Inspect from the menu. Alternatively, you can use the keyboard shortcut F12 or Cmd+Option+I (Mac).
  4. Chrome's DevTools now opens, revealing the browser's diagnostic interface. Every modern browser includes similar developer tools, but Chrome's implementation remains the industry gold standard for debugging, performance analysis, and DOM inspection. Here, you can diagnose issues, monitor network activity, analyze performance bottlenecks, and examine all HTML, CSS, and JavaScript that the browser processes.
  5. The DevTools interface organizes its functionality into tabbed panels, with the Elements panel open by default. This panel provides real-time access to the Document Object Model (DOM).

    In the Elements tab, you can inspect any HTML element and view its complete markup, computed styles, and event listeners. Hover your mouse over any line of HTML code—notice how the corresponding element highlights in the browser window with colored overlays showing padding, margins, and borders. This visual feedback is invaluable for understanding layout relationships and debugging positioning issues.

  6. At the top left of the DevTools window, click the inspector select element element selector button. This activates inspection mode—when you hover over any element in the actual webpage, the corresponding HTML code highlights automatically in DevTools, streamlining the process of locating specific elements in complex documents.
  7. Click the inspector select element button again to deactivate inspection mode.
  8. At the top of the DevTools window, click on the Console tab to access JavaScript's interactive environment.

    The Console serves dual purposes: it displays JavaScript errors, warnings, and log messages from your application, and provides an interactive JavaScript environment where you can execute code in real-time. This makes it an indispensable tool for testing code snippets, debugging functions, and experimenting with DOM manipulation without modifying your source files.

  9. In the Console, type alert('Hello'); and press Return (Mac) or Enter (Windows). This demonstrates the Console's ability to execute JavaScript immediately.
  10. A browser alert dialog displaying Hello appears. Click OK to dismiss it.
  11. The Console enables temporary modifications to any HTML page you're viewing—even live production websites. This capability makes it perfect for rapid prototyping and testing hypotheses about how changes might affect user experience.

    Type document into the Console and press Return (Mac) or Enter (Windows). The document object represents the entire HTML document and serves as the entry point for all DOM manipulation.

  12. The Console returns #document. Hover over this text and observe how the entire webpage highlights, indicating that document encompasses all page content.
  13. Click the arrow next to #document to expand its structure. This reveals the complete DOM tree hierarchy, showing how browsers internally organize HTML into a navigable object structure.
  14. Click the arrow next to <body> to expand it and examine all body contents. This tree structure mirrors your HTML source code but reflects the browser's live interpretation, including any dynamic changes.
  15. Hover over various elements within the <body> section and notice how each content block highlights in the browser window, demonstrating the direct relationship between DOM objects and visual elements.

DevTools Setup Process

1

Open File in Chrome

Navigate to form.html in the Form-Fields folder and open with Google Chrome browser

2

Launch DevTools

Right-click anywhere on the page and select 'Inspect' from the context menu

3

Explore Elements Panel

Use the Elements tab to inspect HTML markup and hover over elements to see highlights

4

Access Console

Switch to the Console tab where you'll execute JavaScript commands for testing

DevTools Navigation Tip

Use the element selector button in DevTools to click on page elements and automatically highlight their corresponding HTML code. This makes it easy to find the exact markup you want to modify.

Selecting & Working with HTML Elements

Now that we understand the document structure, let's explore targeted element selection—a cornerstone skill for building interactive applications. Rather than manipulating entire documents, professional developers work with specific elements using precise selection methods.

  1. At the top left of the DevTools window, click the inspector select element element selector button to reactivate inspection mode.
  2. In the webpage, click on the text field labeled Name. This demonstrates how to identify specific elements for JavaScript targeting.
  3. In DevTools, observe that the input element with ID nameField becomes highlighted in the Elements panel. This ID attribute serves as a unique identifier that JavaScript can reference.
  4. In DevTools, click on the Console tab to return to the JavaScript environment.

  5. In the Console, type document.getElementById('nameField');

    document.getElementById('nameField')
    The fundamental JavaScript method for targeting specific HTML elements by their unique ID attribute. This becomes the foundation for all DOM manipulation.

    DOM Selection Concepts

    Document Object

    Represents the entire HTML page structure. Type 'document' in Console to see the complete page hierarchy and explore its contents.

    Element Targeting

    Use getElementById() with dot syntax to select specific elements by their ID attribute for precise manipulation and control.

About getElementById()

The getElementById() method represents one of the most fundamental DOM selection techniques, allowing precise targeting of any HTML element via its unique ID attribute. Critical note: the method name uses specific capitalization—Id—that must be exact for proper function execution. Writing getElementByID() with an uppercase "ID" will result in a method-not-found error, a common mistake that trips up developers at all skill levels.

  • Press Return (Mac) or Enter (Windows) to execute the selection command.
  • The Console displays <input id="nameField … value="First and Last Name">, representing the complete element object.

    Hover over this output and notice that only the nameField input highlights in the webpage. This confirms successful element selection. We can now access this element's properties and methods using JavaScript's dot notation syntax—a pattern you'll use constantly in professional development.

  • Press the Up Arrow key in the Console to recall your previous command. This command history feature dramatically speeds up iterative development and testing.
  • Extend the previous command by adding the following bold code:

    document.getElementById('nameField').style.display = 'none';

    This statement chains two operations: first, it selects the element with ID nameField, then accesses its CSS display property and sets it to 'none', effectively hiding the element. Note that CSS property values must be provided as strings in JavaScript.

  • Press Return (Mac) or Enter (Windows) to execute the code. The input field disappears instantly, demonstrating real-time DOM manipulation.
  • Let's restore the nameField's visibility. Press the Up Arrow key again to recall the hide command.
  • Modify the display value as shown in bold:

    document.getElementById('nameField').style.display = 'block';
  • Press Return (Mac) or Enter (Windows). The input reappears, confirming your ability to control element visibility programmatically.

  • Critical Syntax Note

    The method must be written as getElementById() with exact capitalization. Writing getElementByID() with capital 'ID' will cause the function to fail completely.

    getElementById() Method Analysis

    Pros
    Precise element targeting using unique ID attributes
    Fast performance for single element selection
    Straightforward syntax for beginners
    Universal browser support across all modern browsers
    Cons
    Requires elements to have unique ID attributes
    Only selects one element at a time
    Case-sensitive method name can cause errors
    Limited to ID-based selection only

    Getting & Setting Input Values

    Input elements form the backbone of user interaction in web applications. Understanding how to programmatically read and modify their values enables dynamic form processing, real-time validation, and sophisticated user interface behaviors that modern applications demand.

    1. Type the following command into the Console:

      document.getElementById('nameField').value;
    2. The Console returns "First and Last Name"—the current text content of the input field. This demonstrates how JavaScript reads user input programmatically.
    3. Let's observe dynamic value changes. Click into the Name field in the webpage and replace the placeholder text with your actual name.
    4. Return to the Console to test JavaScript's ability to detect this user input change.
    5. Press the Up Arrow key to recall the value-reading command.
    6. Press Return (Mac) or Enter (Windows) to re-execute the command.
    7. The Console now displays your entered name, confirming that JavaScript can access real-time user input—a capability essential for form validation, search suggestions, and interactive features.
    8. JavaScript can also programmatically modify input values, enabling features like auto-completion, form pre-population, and data formatting. Press the Up Arrow key again to recall the previous command.
    9. Modify the command to assign a new value, as shown in bold:

      document.getElementById('nameField').value = 'example: John Doe';
    10. Press Return (Mac) or Enter (Windows) to execute the assignment.
    11. The text field now displays example: John Doe, demonstrating programmatic value setting—useful for providing examples, clearing forms, or implementing undo/redo functionality.
    12. Remember that Console-based changes are temporary and development-focused. Click the browser's Reload button to confirm that your experimental changes disappear, returning the page to its original state.

      While Console modifications don't persist across page reloads, this interactive environment provides an invaluable testing ground for developing and debugging JavaScript functionality before implementing it in your production code.

    Getting vs Setting Element Values

    FeatureGetting ValuesSetting Values
    Syntaxelement.valueelement.value = 'new text'
    PurposeRead current contentWrite new content
    Return ValueCurrent string valueUpdated element
    Use CaseForm validationDynamic updates
    Recommended: Use getting for form validation and user input processing. Use setting for dynamic content updates and placeholder management.

    Value Manipulation Workflow

    1

    Select Target Element

    Use document.getElementById() to target the specific input field you want to work with

    2

    Access Value Property

    Add .value to read the current content or assign new content with = operator

    3

    Test Changes

    Execute commands in Console to see immediate results and verify your code works correctly

    Console Testing Benefits

    Changes made in the Console are temporary and lost on page reload. This makes it perfect for testing code safely before implementing permanent solutions in your JavaScript files.

    Key Takeaways

    1Chrome DevTools Console provides a safe environment for testing JavaScript DOM manipulation without affecting permanent code
    2The getElementById() method is the fundamental approach for selecting specific HTML elements using their unique ID attributes
    3Proper capitalization in getElementById() is critical - using getElementByID() with capital 'ID' will cause function failure
    4Element.style.display property controls visibility with values like 'none' for hiding and 'block' for showing elements
    5Input elements have a value property that can be both read to get current content and written to set new content
    6The Up Arrow key in Console recalls previous commands, making it efficient to modify and re-execute JavaScript code
    7Hovering over elements in DevTools highlights corresponding page elements, and vice versa with the element selector tool
    8All modern browsers include Developer Tools for debugging, but Chrome's implementation is particularly robust for JavaScript development

    RELATED ARTICLES