How would you set a custom attribute in HTML?

Prepare for Angular interviews with our comprehensive quiz. Master key concepts and enhance your problem-solving skills with our interactive questions and detailed explanations. Ace your tech interview!

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.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy