How can I get the data-id attribute?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Getting a data-id attribute in the browser is simple once you know which API you want to use. The two most common choices are element.getAttribute("data-id") and element.dataset.id. They both work, but they behave a little differently and are used in slightly different styles of code.
The core idea is that HTML data-* attributes are meant for custom metadata attached to DOM elements. JavaScript can read them directly without needing a separate hidden input or custom parser.
Read data-id with Plain DOM APIs
If you already have the element, getAttribute() is the most explicit method:
This always reads the raw attribute string from the DOM. That makes it a good default when you want direct attribute access and do not need any abstraction.
Use dataset for More Natural Property Access
The dataset API maps data-* attributes to JavaScript properties:
This is often cleaner to read, especially when the code works with several data attributes:
The mapping rule is simple: data-user-id becomes dataset.userId, data-order-status becomes dataset.orderStatus, and so on.
The same API can also write back values:
That updates the DOM attribute as well, which is useful when the page state changes after rendering.
When debugging, this also makes the browser devtools story clearer because you can inspect the updated data-id directly in the rendered HTML.
Read data-id During Event Handling
A very common pattern is reading data-id from the clicked element or one of its ancestors.
This is especially useful with dynamic lists where buttons are created after page load. Instead of binding a listener to every button individually, you let one delegated handler read the data-id from whichever element was clicked.
Know the jQuery Difference
If you are working in older codebases, jQuery offers both .attr() and .data():
These are similar but not identical. .attr() reads the attribute as it appears in the DOM. .data() uses jQuery's data layer, which can cache values and sometimes coerce types. If you want the literal current attribute value, .attr() is usually the safer comparison to the plain DOM APIs.
Common Pitfalls
The biggest mistake is trying to access data-id like a normal property such as element.data-id, which is invalid JavaScript syntax.
Another common issue is forgetting that dataset values are strings. If the data-id represents a number, convert it explicitly before using it in arithmetic or strict comparisons.
It is also easy to read the wrong element in event handlers. event.target may be a child node inside the clickable element, which is why closest() is often the safer choice.
Finally, with jQuery, .data() and .attr() are not always interchangeable once values start changing dynamically.
That difference matters most in older codebases that mix raw DOM and jQuery access.
Summary
- Use
getAttribute("data-id")for direct raw attribute access. - Use
dataset.idfor concise modern DOM code. - Use event delegation and
closest()when readingdata-idfrom clicked elements. - Remember that
data-*values come back as strings. - In jQuery code, choose between
.attr()and.data()deliberately.
Related reading
- How can I get the full object in Node.js''s console.log(), rather than ''[Object]''?
- How can I get the ID of an element using jQuery?
- How can I grab Google Visualization DataTable data after chart is loaded?
- How can I make an AJAX call without jQuery?
- How can I make Bootstrap columns all the same height?
- How can I make the console.log at the bottom wait until its all complete, then show me the answer instead of waiting?
- How can I map a DynamoDB AttributeMap type to an interface?
- How can I pass a parameter to a setTimeout() callback?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.