-1

I want to select the first <d> in <a> and I do not know how many and what non-d elements are in <...>.

Is it possible to select the first <d> with CSS selectors?

<a>
  <...> 
    <d></d>
  </...>
  <b>
    <d></d>
  </b>
</a>

Edit: I think this is impossible and would like to get confirmation about this.

2
  • 1
    In your example markup, you don't have any <a> elements that are inside the <b> element.
    – Paul
    Nov 24, 2018 at 1:44
  • Fixed the title Nov 24, 2018 at 1:47

1 Answer 1

2

It's not possible to match the first descendant of a certain kind within an ancestor if the location of the first descendant cannot be known in advance. jQuery provides the :eq() selector for this, for which there is no standard equivalent.

For example, if you know the first d is always the first child of some other element that's the first (or second or nth) child of a, you can select that, but if it's not always going to appear there, you have to account for all the other possible locations, which you may or may not be able to do depending on how the markup is generated.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.