0
0
HTMLmarkup~5 mins

Data attributes in HTML - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are data attributes in HTML?
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
How do you access the value of data-item-id in JavaScript?
Aelement.dataset.itemId
Belement.data.itemId
Celement.getAttribute('itemId')
Delement.dataItemId
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
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
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>
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.