LearnFrenzy
Sign in Take a free test

DOM Manipulation · Find the Error · Question 2

Error: a dashed name on dataset Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The page contains this HTML:

<div id="profile"></div>

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const profile = document.getElementById("profile");
profile.dataset.user-id = "42";
  • A It sets data-user-id="42"
  • B It sets data-user="42"
  • C ReferenceError: id is not defined
  • D SyntaxError: it parses as profile.dataset.user - id = "42", an invalid assignment target – use dataset.userId Correct
Correct Answer: Option D

dataset uses camelCase (dataset.userId) or bracket notation with the camelCase key. A subtraction cannot be assigned to.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links