While code reading I came across a piece of code in JavaScript which set the placeholder
attribute for an input
element ($("#el").attr("placeholder", "I am Input");
). I myself have always set such attributes in the HTML markup itself rather than relying on JS for this (<input id="el" type="input" placeholder="I am Input" />
).
My question is which is the better way to go about it? I know that there is no performance hit if we go for JS, but code practices wise I always felt that these type of attributes should be set in HTML markup, unless it is dynamic based on some other parameters.
Couple of reasons I felt this way is, in general this leads nicely to the principal of graceful degradation when no JS is allowed. And other is, this plays nicely for when we are using shims to imitate placeholder
UX in browsers which do not support it natively.
Would love to hear what you guys think about it. Any links to coding conventions/best practices also welcome.