I believe jQuery does not count/iterate textnode. Maybe there's a
function in jQuery but I have no idea.

On Dec 30, 12:52 pm, nachocab <nacho...@gmail.com> wrote:
> Thank you both,
> The only problem is that the right answer isn't 2, it's 3. I'm
> actually looking for the index of item#my_item inside the contents
> array. children() doesn't take into consideration the "!!" textnode.
>
> On Dec 30, 9:43 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > var $items = $('<items><item>Hello</item><item>world</item><item
> > id="my_item">Goodnight moon!</item></items>');
> > var $my = $items.find('#my_item');
> > console.info( $items.children().index($my) );
>
> > or
>
> > var $items = $('<items><item>Hello</item><item>world</item><item
> > id="my_item">Goodnight moon!</item></items>');
> > $items.children().each(function(i){
> >    if ( this.id == 'my_item' ) console.info(i);
>
> > });
>
> > indexOf is a method of the String object, no use in this case.
>
> > On Dec 30, 6:07 pm, nachocab <nacho...@gmail.com> wrote:
>
> > > Actually that gives an error. It should be:
> > > var $items = $('<items><item>Hello</item><item>world</item>!!<item
> > > id="my_item">Goodnight moon!</item></items>');
> > > for(var i=0; i <= $items.children().length; i++){
> > >     if ( $items.contents()[i].id == 'my_item' )
> > >         console.info(i)}
>
> > > It also needs to work with $items.contents()[i].className
>
> > > On Dec 30, 8:30 pm, nachocab <nacho...@gmail.com> wrote:
>
> > > > Hi everyone,
> > > > I'd like to know if there's a way to do this without using a for loop.
> > > > You can just paste this in Firebug:
>
> > > > var $items = $('<items><item>Hello</item><item>world</item>!!<item
> > > > id="my_item">Goodnight moon!</item></items>');
> > > > for(var i=0; i <= $items.children().length; i++){
> > > >     if ( $items.contents()[i].id == 'my_item' )
> > > >         console.info(i)
>
> > > > }
>
> > > > Keep in mind that I need to use contents() to also get the text node
> > > > ("!!"). I can't seem to use contents().index() properly.
>
> > > > Thanks,
>
> > > > Nacho

Reply via email to