0

What if I want to use the for attribute on an element other than a <label>? Is this valid HTML? For example:

<div class="feedback" for="my-input-box"></div>

The idea is, I can use JavaScript to get the feedback container for an input using its ID. Like so:

var feedbackContainer = document.querySelector("[for='" + myInputBoxID + "']'");

I just want to know if this is valid HTML or not. If not, I can obviously just go for data-for="..." instead.

darkhorse
  • 8,192
  • 21
  • 72
  • 148

2 Answers2

3

That is not valid HTML.

See on W3Schools that the for="" attribute only is used on <label> and <output>.

Jelmer Overeem
  • 359
  • 1
  • 10
3

The HTML for attribute is only valid on label and output elements.

See - https://www.w3schools.com/tags/att_for.asp

So to answer your question, that is invalid HTML.

Glen Carpenter
  • 392
  • 2
  • 9