I'm trying to find out the "best practice" for getting the contents of
one particular element.  I know about the each() call.

As I said I can use ways that seem not so efficient to get the
contents of a single element but I want to know the best way.

On Jul 1, 5:20 pm, Mike Alsup <[EMAIL PROTECTED]> wrote:
> > I am using jquery to parse some xml.  I'm looking for the best
> > practice to access an elements text contents.
>
> > Let's say my xml looks like this:
>
> > <items>
> >   <item>
> >      Hello world!
> >    </item
> >    <item>
> >       Goodnight moon!
> >    </item>
> > <items>
>
> > I can get a wrapped set of the elements this way: $('items > item',
> > xml)
>
> > And I can get the text of element 1 this way:  $('items > item:eq(1)',
> > xml).text()
>
> > But how do I get the contents once I am directly accessing the element
> > instead of the jquery object?
>
> > In other words I'd like to do something like this: $('items > item',
> > xml)[1].text(), but since the element that’s returned from the array
> > [] doesn’t support text() how do I get it’s contents?
>
> > I know I can wrap it in a second $ call, as in $($('items > item', xml)
> > [1]).text() but that seems less than ideal.
>
> Why do you need to access the element?  Are you trying to get the
> contents of each 'item'?  If so, here's a way to do that:
>
> $('items > item').each(i,o) {
>     // in this loop 'i' is the index of the loop iteration
>     // and 'o' is the object of the iteration (item element in this
> case)
>     var $item = $(o); //  could also do:  var $item = $(this);
>     var txt = $item.text();
>
> });

Reply via email to