Data attributes are custom attributes that start with data- and allow you to store extra information on HTML elements without affecting the layout or functionality.
Click to reveal answer
beginner
How do you write a data attribute for a user's ID on a <div> element?
You write it like this: <div data-user-id="123"></div>. The attribute name is data-user-id and the value is 123.
Click to reveal answer
intermediate
How can JavaScript access the value of a data attribute?
JavaScript can access data attributes using the dataset property. For example, element.dataset.userId gets the value of data-user-id.
Click to reveal answer
intermediate
Why use data attributes instead of classes or IDs for storing extra info?
Data attributes store extra info without affecting styles or behavior. Classes and IDs are for styling and identifying elements, not for storing custom data.
Click to reveal answer
beginner
Can data attribute names contain uppercase letters or special characters?
No. Data attribute names must be lowercase and can only contain letters, numbers, and hyphens. They cannot contain uppercase letters or special characters.
Click to reveal answer
Which of these is a valid data attribute?
Adata_user_name
Bdata-user-name
Cuser-data-name
DdataUserName
✗ Incorrect
Data attributes must start with 'data-' and use lowercase letters and hyphens only. 'data-user-name' is correct.
How do you access the value of data-item-id in JavaScript?
Aelement.dataset.itemId
Belement.data.itemId
Celement.getAttribute('itemId')
Delement.dataItemId
✗ Incorrect
JavaScript uses the 'dataset' property with camelCase for multi-word names: 'data-item-id' becomes 'itemId'.
What is the main purpose of data attributes?
ATo store extra custom data on HTML elements
BTo style elements with CSS
CTo identify elements uniquely
DTo add event listeners
✗ Incorrect
Data attributes store extra information on elements without affecting style or behavior.
Which is NOT true about data attributes?
AThey can be accessed via JavaScript
BThey start with 'data-'
CThey affect the visual style of elements by default
DThey can store custom information
✗ Incorrect
Data attributes do not affect the visual style unless CSS or JavaScript uses them explicitly.
Which is a correct way to add a data attribute for a product price?
A<div data_price="19.99"></div>
B<div price-data="19.99"></div>
C<div dataPrice="19.99"></div>
D<div data-price="19.99"></div>
✗ Incorrect
Data attributes must start with 'data-' and use hyphens, not camelCase or underscores.
Explain what data attributes are and how they can be used in HTML and JavaScript.
Think about how you can add hidden info to elements without changing their look.
You got /4 concepts.
Describe the rules for naming data attributes and why these rules matter.
Consider what happens if you use uppercase or spaces in attribute names.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of data- attributes in HTML?
easy
A. To change the color of HTML elements
B. To store extra information inside HTML elements without affecting their appearance
C. To create links between pages
D. To add comments inside HTML code
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 B
Quick Check:
Data attributes store extra info = B [OK]
Hint: Data attributes always start with 'data-' and hold extra info [OK]
Common Mistakes:
Thinking data attributes change element style
Confusing data attributes with links or comments
Believing data attributes are visible on the page
2. Which of the following is the correct way to add a data attribute to a <div> element?
easy
A.
B.
C.
D.
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 D
Quick Check:
Correct data attribute syntax = D [OK]
Hint: Data attributes start with 'data-' and use hyphens, not underscores [OK]
What will button.dataset.action return in JavaScript?
medium
A. "save"
B. "Save"
C. "data-action"
D. undefined
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 A
Quick Check:
dataset.action returns attribute value = A [OK]
Hint: dataset.property matches data-attribute name after 'data-' [OK]
Common Mistakes:
Confusing attribute value case
Trying to access with full 'data-action' name
Expecting undefined if attribute exists
4. Identify the error in this HTML snippet:
<span data-user_id="7">User</span>
medium
A. The attribute name uses an underscore instead of a hyphen
B. The value '7' should be in single quotes
C. The <span> tag cannot have data attributes
D. Missing closing tag for <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 A
Quick Check:
Data attribute names use hyphens, not underscores = C [OK]
Hint: Data attribute names must use hyphens, never underscores [OK]
Common Mistakes:
Using underscores in data attribute names
Thinking quotes must be single
Believing some tags can't have data attributes
5. You want to store a user's preferred language and theme inside a <section> element using data attributes. Which of these is the best and valid way to do it?
hard
A.
B.
C.
D.
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 C
Quick Check:
Valid data attributes start with 'data-' and use hyphens = A [OK]
Hint: Use 'data-' prefix and hyphens for multiple words in attribute names [OK]
Common Mistakes:
Using camelCase or underscores in data attribute names
Omitting the 'data-' prefix
Trying to store data without data attributes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.