I know the difference between aria-label and aria-labelledby, but what's the differences between those below, and when to use one from another?
The code is from react-testing-library
// first
<label for="username-input">Username</label>
<input id="username-input" />
// second
<label id="username-label">Username</label>
<input aria-labelledby="username-label" />
For the first one, according to the mdn documentation
The label text is not only visually associated with its corresponding text input; it is programmatically associated with it too. ... You can click the associated label to focus/activate the input, as well as the input itself.
But the second one does not provide that functionality for browser user, since its just an ARIA attribute for the accessibility.
I also inpected chrome devtools - accessiblity, and I found that both first and second's computed properties show that input and label are associated to each other.
So as of my understanding, first one gives user programatical access but second one does not, and both first one and second one are accessed by screen reader properly.
Is second one subset of the first one? so is below code invalid pattern since the first one is a superset of second one?
<label id="username-label" for="username-input">Username</label>
<input id="username-input" aria-labelledby="username-label" />
Thank you.