Skip to content

Element#index and Element#indexOfType #220

@jwestbrook

Description

@jwestbrook

previous lighthouse ticket #1357
by Michael Morris


I find myself needing to figure out an element's ordering among its siblings often enough to consider a universal method for it. Consider the following DOM

<ul>
  <li id="first">Apple</li>
  <li id="second">Orange</li>
  <li id="third">Pear</li>
</ul>

The command $('first').index() would return 0, that element's position among its siblings. However, this function wouldn't consider tag types by design. Faced with a mixed tag type collection it still returns the index, but if the user wants nth-of-type they would have to use the proposed sibling function indexOfType.

<div>
  <div id="first">Hello there</div>
  <section id="second"> Sectioning</section>
  <article id="third"> articles</article>
  <div id="fourth"> and so on.</div>
</div>

With the DOM above $('fourth').index() returns "3". $('fourth').indexOfType() returns "1" since it is the second div tag in the group.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions