Prepare for Angular Interviews with real-life questions. Utilize quizzes and examples to deepen understanding and enhance your skills. Gear up to ace your interview!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How would you set a custom attribute in HTML?

  1. Add a property directly in JavaScript

  2. Prefix it with "data-" in the HTML

  3. Use the setAttribute() method in JavaScript

  4. Include it in the style tag

The correct answer is: Prefix it with "data-" in the HTML

Setting a custom attribute in HTML is done by prefixing the attribute with "data-". This is part of the HTML5 specification, which allows developers to embed custom data attributes onto HTML elements. For example, if you want to create a custom attribute to store user information, you could write `<div data-user-id="12345"></div>`. This way, the attribute `data-user-id` can be easily accessed via JavaScript (e.g., using `element.getAttribute('data-user-id')`), allowing you to interact with the stored value while keeping the HTML valid and semantic. Using the "data-" prefix ensures that the browser understands that these attributes are intended for custom use and not conflicting with standard HTML attributes. This approach promotes best practices in web development by keeping custom attributes standardized and easy to manage.