This question has to do with the way selectors work and is not a problem I am having. I was working with some HTML similar to this:
<div class="example">
<textarea>...</textarea>
<textarea for="1">foo</textarea>
<textarea for="2">bar</textarea>
<textarea for="3">hello</textarea>
<textarea for="4">world</textarea>
</div>
I was trying to use $('.example').children('textarea[for]:nth-of-type(1)')
to select the first textarea with the for
attribute. I kept getting undefined. I reread the documentation and noticed the line saying
Selects all elements that are the nth child of their parent in relation to siblings with the same element name.
And it makes sense that textarea[for]:nth-of-type(1)
would be returning undefined because the first textarea doesn't have the for
attribute.
My question then is, would it be possible in the future for an element[attribute]:nth-of-type(n)
selector to return the nth element with the specified attribute? Would this require a whole new selector because of the way jQuery/CSS work?