Access DOM elements JavaScript


show more How do you access DOM elements with JavaScript? Find out how in this JavaScript tutorial. Simon Allardice shows how to target HTML elements with IDs (using document.getElementById) and by name (using document.getElementsByTagName), as well as how to access the node type and child elements of a node. show less
please wait ...

Accessing DOM elements

So how do we write JavaScript to grab hold of one of these nodes? And well, it really depends. The main question you have to ask is, is it unique? The thing that you're after, is it unique in the document, and that means does it have an ID? If I'm after, say, an unordered list, I need to ask the question, does it have an ID in the HTML? You know that you can add the ID and class attributes to any of your HTML elements, but that a class can be added to multiple elements, but an ID should only be applied to one. It should be unique.

And if the element that you're after has that unique ID, you can grab it in JavaScript by using possibly the most important method of the document object, which is this one, document.getElementById, and you pass in the ID of that HTML element. Now notice the uppercase and lowercase mixture here. It is, of course, case sensitive. So document is lowercase, then a dot, then getElementById. get is lowercased. Every subsequent word begins with uppercase.

Ele...

Accessing DOM elements
Video duration: 11m 3s 5h 31m Beginner

Viewers:

How do you access DOM elements with JavaScript? Find out how in this JavaScript tutorial. Simon Allardice shows how to target HTML elements with IDs (using document.getElementById) and by name (using document.getElementsByTagName), as well as how to access the node type and child elements of a node.

Subjects:
Developer Web
Software:
JavaScript
Author:
please wait ...