I noticed some errors in my code (it was written directly...), so here
is the corrected one:
jQuery.inArray = Array.indexOf ?
function(elem, array, i) {
return Array.indexOf(array, elem, i);
} :
function(elem, array, i) {
var l = array.length;
i = i === undefined ? 0 :
i < 0 ? Math.ceil(i) + l :
Math.floor(i);
for (; i < l; ++i)
if ( array[i] === elem )
return i;
return -1;
};
jQuery.lastInArray = Array.lastIndexOf ?
function(elem, array, i) {
return Array.lastIndexOf(array, elem, i);
} :
function(elem, array, i) {
var l = array.length
i = i === undefined ? l-1 :
i < 0 ? Math.ceil(i) + l :
Math.floor(i);
for (; i > -1; --i)
if ( array[i] === value )
return i;
return -1;
}
Hope it is ok now...
On Jan 23, 9:45 am, Robert Katić <[email protected]> wrote:
> Maybe it would be better to return the index of the element in a new
> jQuery obj from selector only if elem === undefined....
>
> Regardin jQuery.inArray maybe it would be useful to heve an
> jQuery.lastInArray too. Maybe with start/end index as the thrd
> argument?
> Something like this:
>
> jQuery.inArray = Array.indexOf ? //some browsers have this
> function(elem, array, i) {
> return Array.indexOf(array, elem, i);
> } :
> function(array, elem, i) {
> var l = array.length;
> i = i === undefined ? 0 :
> i < 0 ? Math.ceil(i) + l :
> Math.floor(i);
> for (; i < l; ++i)
> if (array[i] === elem)
> return i;
> return -1;
> };
>
> jQuery.lastInArray = Array.lastIndexOf ? //some browsers have this
> function(elem, array, i) {
> return Array.lastIndexOf(array, elem, i);
> } :
> function(elem, array, i) {
> var l = array.length
> i = i === undefined ? l-1 :
> i < 0 ? Math.ceil(i) + l :
> Math.floor(i);
> while ( i )
> if ( seq[--i] === value )
> return i;
> return -1;
> }
>
> Thoughts?
>
> On Jan 23, 3:21 am, John Resig <[email protected]> wrote:
>
> > Yeah, I can dig this. You should file an enhancement ticket.
>
> > --John
>
> > On Thu, Jan 22, 2009 at 9:10 PM, ajpiano <[email protected]> wrote:
>
> > > It would be useful to be able to use the .index() method to find the
> > > index of the current element in a given set, rather than the current
> > > implementation (searching the current set for a given element), which
> > > forces the developer to do gyrations in order to find out a simple
> > > piece of info.
>
> > > For instance
>
> > > $("#sometable th img").click(function() {
> > > //find out index of img's parent <th> in its own <tr>
> > > var $th = $(this).parent();
> > > //now we have a few not so great choices
> > > //1.
> > > var index = $("#sometable thead tr).index($th);
> > > //2.
> > > var index = $th.parent().children().index($th);
> > > //3. doesn't even work, but many would assume it would, and it
> > > doesn't!)
> > > // because the desired element is always the last one in the set.
> > > var index = $th.siblings().andSelf().index($th);
> > > });
>
> > > Paul Irish and I propose the following enhancement, which is rough and
> > > we welcome any suggestions...
>
> > > jQuery.fn.index = function(elem){
>
> > > // legacy implementation
> > > if ( typeof elem === 'object'){
> > > return jQuery.inArray(
> > > elem && elem.jquery ? elem[0] : elem
> > > , this );
> > > }
> > > //return the index of the element in a new jQuery obj from selector,
> > > or by default, amongst its own siblings.
> > > return jQuery.inArray(this[0],
> > > elem ? jQuery(elem) : this.parent().children() );
> > > };
>
> > > which would allow for the following.
>
> > > $("#sometable th img").click(function() {
> > > //find out index of img's parent <th> in its own <tr>
> > > var index = $(this).parent().index();
>
> > > //for sake of example, index of image in set of all images.
> > > var imgIndex = $(this).index("img");
> > > });
>
> > > If people think this is worthwhile, we will post an enhancement
> > > ticket. Thoughts?
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---