JavaScript Element

Beginner
8 min read
Updated: Nov 2026
3 examples
DOM elements

Definition and Usage

An Element is a DOM node that represents an HTML (or XML) tag, such as <button> or <div>. Elements have a tag name, attributes, and can contain other nodes.

You change attributes with getAttribute / setAttribute, manage CSS classes with classList, and search inside an element with element.querySelector(). Element extends Node.

This page teaches what Element is. Method, property, and event guides live under the Element section.

Elements are nodes with a tag.

Text nodes are Nodes but not Elements. Only Element nodes have attributes and tagName.

JavaScript Syntax

Common Element operations:

JavaScript
const btn = document.querySelector("button");
btn.setAttribute("type", "button");
btn.classList.add("primary");
console.log(btn.tagName); // "BUTTON"

Core Ideas

Ideas every JavaScript Element tutorial builds on:

IdeaExampleRemember
Tagel.tagNameUppercase for HTML
Attributeel.getAttribute("id")String attributes
classListel.classList.add("on")Add/remove/toggle classes
Search insideel.querySelector("span")Scoped to that element
Childrenel.childrenElement children only
Eventsel.addEventListener(...)From EventTarget

JavaScript Examples

Three short programs that show how Element works in a JS tutorial.

Example 1 — Read and write an attribute

Attributes are stored as strings on the element.

JavaScript
const input = document.createElement("input");
input.setAttribute("type", "email");
console.log(input.getAttribute("type"));

How It Works

  1. createElement builds an input element.
  2. setAttribute writes the type attribute.
  3. getAttribute reads it back as "email".

Example 2 — Toggle a CSS class

classList is the safe way to add and remove classes.

JavaScript
const box = document.createElement("div");
box.classList.add("card");
box.classList.toggle("open");
console.log(box.className);

How It Works

  1. add("card") adds the first class.
  2. toggle("open") adds open because it was missing.
  3. className shows both class names as a string.

Example 3 — Check the tag name

In HTML documents, tagName is uppercase.

JavaScript
const p = document.createElement("p");
console.log(p.tagName);
console.log(p.tagName === "P");

How It Works

  1. createElement("p") creates a paragraph element.
  2. tagName is "P" in HTML.
  3. Compare with uppercase when you branch on the tag.

Next: Element Guides

After you know what Element is, open method and property pages for the full API.

Look upWhy
querySelector()Find a descendant
getAttribute()Read an attribute string
setAttribute()Write an attribute
classListManage CSS classes

Full lists: querySelector().

Frequently Asked Questions

An Element is a DOM node for a tag such as div or button. It has a tagName, attributes, and can contain child nodes.
Every Element is a Node, but not every Node is an Element. Text and comment nodes are Nodes without attributes or tagName.
classList is safer for adding and removing one class at a time. className replaces the whole class string.
For HTML elements it is usually uppercase, such as "DIV". Compare carefully or use lowercase normalization.
Open Element method pages such as querySelector, then Node and Document for tree operations.

Open an Element method

Start with querySelector(), then explore attributes and classList.

querySelector() →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful