Chapter 1 deck Chapter 2 deck Chapter 3 deck Chapter 4 deck Chapter 5 deck Chapter 6 deck Chapter 7 deck Chapter 8 deck Chapter 9 deck Chapter 10 deck Chapter 11 deck Chapter 12 deck Chapter 13 deck Chapter 14 deck

Finding & Accessing HTML Elements

8 slides · use ←/→ or Space to navigate, F for fullscreen.

Reading view

← Back to the full Chapter 6 deck

Finding & Accessing HTML Elements
Fullscreen for presentation · Print / PDF for a handout
1 / 8
Lesson

Finding & Accessing HTML Elements

getElementById, getElementsByTagName, getElementsByClassName, querySelectorAll and form collections.

Finding & Accessing HTML Elements

The Document Object: Finding HTML Elements

To access any element, always start with the document object.

MethodDescription
document.getElementById(id)Find an element by its id attribute.
document.getElementsByTagName(name)Find elements by HTML tag name.
document.getElementsByClassName(name)Find elements by CSS class name.
document.querySelectorAll(selector)Find elements by CSS selector.
document.forms[name].elements[i].valueAccess HTML form object collections.
Finding & Accessing HTML Elements

Finding Elements by ID — getElementById()

Returns the element whose id matches the given string. The id should be unique; returns null if not found.

<p id="intro">Hello World!</p>
<script>
  var x = document.getElementById("intro");
  document.write("Paragraph text: " + x.innerHTML);
</script>
Finding & Accessing HTML Elements

Finding Elements by Tag Name — getElementsByTagName()

Returns an HTMLCollection of all elements with the given tag name — access using a zero-based index.

<p>Hello World!</p>
<p>The DOM is very useful!</p>
<script>
  var x = document.getElementsByTagName("p");
  document.write("First paragraph: " + x[0].innerHTML);
</script>
Finding & Accessing HTML Elements

Finding Elements by Class Name — getElementsByClassName()

<p class="intro">The DOM is very useful.</p>
<p class="intro">This demonstrates getElementsByClassName.</p>
<p id="demo"></p>
<script>
  var x = document.getElementsByClassName("intro");
  document.getElementById("demo").innerHTML = "First intro paragraph: " + x[0].innerHTML;
</script>
Finding & Accessing HTML Elements

Finding Elements by CSS Selector — querySelectorAll()

Selects all elements matching a CSS selector string and returns a static NodeList — more powerful and flexible than the class/tag methods.

<p class="intro">The DOM is very useful.</p>
<p class="intro">This demonstrates querySelectorAll.</p>
<p id="demo"></p>
<script>
  var x = document.querySelectorAll(".intro");
  document.getElementById("demo").innerHTML = "First match: " + x[0].innerHTML;
</script>
Finding & Accessing HTML Elements

Finding Elements via HTML Object Collections

<form id="frm1">
  First name: <input type="text" name="fname" value="Donald">
  Last name:  <input type="text" name="lname" value="Duck">
  <input type="submit" value="Submit">
</form>
<script>
  function myFunction() {
    var x = document.forms["frm1"];
    var text = "";
    for (var i = 0; i < x.length; i++) {
      text += x.elements[i].value + "<br>";
    }
    document.getElementById("demo").innerHTML = text;
  }
</script>
Finding & Accessing HTML Elements

Activity — complete the code

Complete the four ways of finding elements.

// By id
var x = document.("intro");
 
// By tag name (returns a collection)
var y = document.("p");
document.write(y[0].innerHTML);
 
// By class name
var z = document.("intro");
 
// By CSS selector (returns a NodeList)
var w = document.(".intro");