I tested adding arbitrary fields to HTML elements in JavaScript and it worked as expected:
const element = document.getElementById('element-id');
element.greet = (message) => { alert(message); };
element.message = 'Hello, world!';
element.greet(element.message);
The alert shows up with the message, but I’m concerned:
- Do all the major browsers support this behavior?
- Is there a pattern for choosing new field name to make sure it won’t conflict with names that the browsers use as fields for the elements?
- Is there any reason I should avoid adding arbitrary fields to HTML elements?