Home JavaScript Element JavaScript Element Definition
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.
Syntax
JavaScript Syntax Common Element operations:
const btn = document.querySelector("button");
btn.setAttribute("type", "button");
btn.classList.add("primary");
console.log(btn.tagName); // "BUTTON" Reference
Core Ideas Ideas every JavaScript Element tutorial builds on:
Idea Example Remember Tag el.tagNameUppercase for HTML Attribute el.getAttribute("id")String attributes classList el.classList.add("on")Add/remove/toggle classes Search inside el.querySelector("span")Scoped to that element Children el.childrenElement children only Events el.addEventListener(...)From EventTarget
Code
JavaScript Examples Three short programs that show how Element works in a JS tutorial.
1. Attributes 2. classList 3. tagName Example 1 — Read and write an attribute Attributes are stored as strings on the element.
const input = document.createElement("input");
input.setAttribute("type", "email");
console.log(input.getAttribute("type")); How It Works createElement builds an input element.setAttribute writes the type attribute.getAttribute reads it back as "email".Example 2 — Toggle a CSS class classList is the safe way to add and remove classes.
const box = document.createElement("div");
box.classList.add("card");
box.classList.toggle("open");
console.log(box.className); How It Works add("card") adds the first class.toggle("open") adds open because it was missing.className shows both class names as a string.Example 3 — Check the tag name In HTML documents, tagName is uppercase.
const p = document.createElement("p");
console.log(p.tagName);
console.log(p.tagName === "P"); How It Works createElement("p") creates a paragraph element.tagName is "P" in HTML.Compare with uppercase when you branch on the tag. Continue
Next: Element Guides After you know what Element is, open method and property pages for the full API.
Look up Why querySelector() Find a descendant getAttribute()Read an attribute string setAttribute()Write an attribute classListManage CSS classes
Full lists: querySelector() .
Frequently Asked Questions What is an Element in JavaScript? An Element is a DOM node for a tag such as div or button. It has a tagName, attributes, and can contain child nodes.
How is Element different from Node? Every Element is a Node, but not every Node is an Element. Text and comment nodes are Nodes without attributes or tagName.
Should I use className or classList? classList is safer for adding and removing one class at a time. className replaces the whole class string.
What does tagName return? For HTML elements it is usually uppercase, such as "DIV". Compare carefully or use lowercase normalization.
Where should I go next? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion