Choosing a specific page element

Objective

  • Select and modify a specific page element

Using document.getElementById

  • there are a couple of easy ways to work with a specific element
    • place JavaScript code in the element's opening tag as the value of an event handler
    • give the element an id value and use the document.getElementById method to access that element
  • using getElementById lets you remove JavaScript code from being part of the content of the page
  • see onclick1.html for an example of inline JavaScript
  • see onclick2.html for equivalent JavaScript using document.getElementById
  • see onclick5.html for an example of changing a specific elements content continuously
  • libraries such as jQuery greatly expand the ability to locate specific elements or a set of elements
  • but... ECMAScript 6 additions make external libraries such as jQuery less needed