Data attributes let you store extra information inside HTML elements without changing how they look. This helps you keep data connected to parts of your page.
Data attributes in HTML
<element data-key="value">Content</element>
Data attributes always start with data- followed by your custom name.
The value is always a string inside quotes.
data-action holding the value "save".<button data-action="save">Save</button>
<div data-user-id="12345">User Info</div>
<img src="photo.jpg" data-description="A sunny beach">
This page has three buttons. Each button has a data attribute data-action with a different value. When you click a button, a message shows which action you clicked, using the data attribute value.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Data Attributes Example</title> <style> button { font-size: 1.2rem; padding: 0.5rem 1rem; margin: 0.5rem; cursor: pointer; } </style> </head> <body> <button data-action="save">Save</button> <button data-action="delete">Delete</button> <button data-action="edit">Edit</button> <script> const buttons = document.querySelectorAll('button'); buttons.forEach(button => { button.addEventListener('click', () => { alert(`You clicked the ${button.dataset.action} button.`); }); }); </script> </body> </html>
You can access data attributes in JavaScript using element.dataset.key, where key is the part after data- converted to camelCase.
Data attribute names should be lowercase and use dashes for readability, like data-user-id.
Data attributes do not affect the page style or layout by themselves.
Data attributes store extra info inside HTML elements without changing their look.
They always start with data- and can be read easily with JavaScript.
Use them to keep data connected to page parts for scripts or styling.
Practice
data- attributes in HTML?Solution
Step 1: Understand what data attributes do
Data attributes store extra information inside HTML elements without changing how they look on the page.Step 2: Compare options with this purpose
Only To store extra information inside HTML elements without affecting their appearance describes storing extra info without affecting appearance, which matches the purpose of data attributes.Final Answer:
To store extra information inside HTML elements without affecting their appearance -> Option BQuick Check:
Data attributes store extra info = B [OK]
- Thinking data attributes change element style
- Confusing data attributes with links or comments
- Believing data attributes are visible on the page
<div> element?Solution
Step 1: Recall the correct syntax for data attributes
Data attributes must start with 'data-' followed by a name using hyphens, not underscores or camelCase.Step 2: Check each option
uses 'data-user' which is correct. Options A, B, and C use invalid formats like missing 'data-', underscores, or camelCase.Final Answer:
<div data-user="123"></div> -> Option DQuick Check:
Correct data attribute syntax = D [OK]
- Using underscores instead of hyphens
- Omitting the 'data-' prefix
- Using camelCase instead of hyphen-case
<button data-action="save" data-id="42">Save</button>
What will
button.dataset.action return in JavaScript?Solution
Step 1: Understand how dataset works in JavaScript
The dataset property accesses data attributes by their name after 'data-' in camelCase. Here, 'data-action' becomes 'action'.Step 2: Check the value of data-action attribute
The attribute value is "save" (all lowercase), so button.dataset.action returns "save" exactly.Final Answer:
"save" -> Option AQuick Check:
dataset.action returns attribute value = A [OK]
- Confusing attribute value case
- Trying to access with full 'data-action' name
- Expecting undefined if attribute exists
<span data-user_id="7">User</span>
Solution
Step 1: Check data attribute naming rules
Data attribute names must use hyphens, not underscores, after 'data-'. Underscores are invalid.Step 2: Verify other parts of the code
The value can be in double or single quotes, <span> can have data attributes, and the closing tag is present.Final Answer:
The attribute name uses an underscore instead of a hyphen -> Option AQuick Check:
Data attribute names use hyphens, not underscores = C [OK]
- Using underscores in data attribute names
- Thinking quotes must be single
- Believing some tags can't have data attributes
<section> element using data attributes. Which of these is the best and valid way to do it?Solution
Step 1: Recall correct data attribute naming conventions
Data attributes must start with 'data-' and use hyphen-separated lowercase names.Step 2: Evaluate each option
uses correct 'data-preferred-language' and 'data-theme'. Options B and C use invalid camelCase or underscores. misses 'data-' prefix.Final Answer:
<section data-preferred-language="en" data-theme="dark"></section> -> Option CQuick Check:
Valid data attributes start with 'data-' and use hyphens = A [OK]
- Using camelCase or underscores in data attribute names
- Omitting the 'data-' prefix
- Trying to store data without data attributes
Ready to prove it?
Pass the test and this pattern moves unknown
