#1357 new
Michael Morris

Element#index and Element#indexOfType

Reported by Michael Morris | June 11th, 2012 @ 03:56 PM

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

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

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 id="first">Hello there</div>
  <section id="second"> Sectioning</section>
  <article id="third"> articles</article>
  <div id="fourth"> and so on.</div>

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


Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket