0

I have a <div> displaying the result fetched from backend. This <div> contains a bunch of children <span>s, with each corresponding to a named value from the backend returned result. Like this:

<div id="result-display">
  <span name="name-one"></span>
  <span name="name-two"></span>
</div>

For each updating cycle, the script just looping through all the <span>s with an attribute of name, and fill the value into it.

In testing, it works. But I've found out that using name attribute for elements other than input is considered bad practice. Why?

Jinghui Niu
  • 990
  • 11
  • 28
  • ` – connexo Apr 09 '21 at 08:38
  • If I never try to please the validator, does it matter? It works perfectly though. – Jinghui Niu Apr 09 '21 at 13:34
  • It may work today in browsers x, y and z. It can stop working tomorrow, or in 2 years, at basically at any point in time. It is always a bad idea to write code based on the error tolerance of systems, to rely on *unspecified* behaviour. There is wide-spread agreement on invalid HTML not being an option, *ever*. Also, why would you insist on invalid HTML when there are valid, cheap alternatives? – connexo Apr 09 '21 at 13:40
  • @connexo I found your advice very enlightening. I've never thought of this point. Why don't you make it a formal answer? I'll adopt that. – Jinghui Niu Apr 09 '21 at 13:45
  • Your question already has an answer in the duplicate. – connexo Apr 09 '21 at 13:48
  • @connexo Yeah, never thought that a `name` without the prefex `data-` would be automatically considered a `customer`. Very enlightening! – Jinghui Niu Apr 09 '21 at 13:49
  • custom !== customer – connexo Apr 09 '21 at 13:51
  • @connexo That was a typo. Yes, no kidding, this one is a very useful tip for me! – Jinghui Niu Apr 09 '21 at 13:56

0 Answers0