[jQuery] Re: Noob question about accessing element contents
This forum is about using jQuery, thus I assumed that I didn't have to spell out that I indeed of course meant the best jquery way. Thank you for your answer.
[jQuery] Re: Noob question about accessing element contents
One answer: Use the technique you mentioned, or any of the techniques the others suggested. They will all work and are all fine to use. Another answer: In code that is performance critical, you may want to bypass all that and go right to the metal. Each of your "item" elements contains a single text node, whose nodeValue is the text. So, if you have an item refernece, item.childNodes[0].nodeValue is the text value, or even better, item.firstChild.nodeValue. A test you can run in Firebug: var $items = $('Hello world!Goodnight moon!'); var item = $items.find('item')[1]; // or any other code to get a single item, and then: alert( item.firstChild.nodeValue ); // "Goodnight moon!" This will change if there are other elements nested inside an item, but you can always poke around in Firebug to see what's what. Do a console.log of the node you're interested in, and look at its properties. -Mike > 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: > > > > Hello world! > > > Goodnight moon! > > > > 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. > > Thanks! >
[jQuery] Re: Noob question about accessing element contents
This forum is about using jQuery, so our answers will usually be focused on the best JQUERY way, not necessarily the BEST way. On Jul 2, 11:51 am, ml1 <[EMAIL PROTECTED]> wrote: > 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: > > > > > > > > > > Hello world! > > > > > > > > Goodnight moon! > > > > > > > > > > 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(); > > > });
[jQuery] Re: Noob question about accessing element contents
Let's say you've got the element stored in a variable as an XMLElement, then it would be a simple matter to refer to the jQuery object of the element itself, for example, for variable elem: $(elem,xml).text() On Jul 2, 12:10 pm, "Richard D. Worth" <[EMAIL PROTECTED]> wrote: > $('item', xml).eq(1).text() > > - Richard > > On Wed, Jul 2, 2008 at 11:51 AM, ml1 <[EMAIL PROTECTED]> wrote: > > > 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: > > > > > > > > > > > > > Hello world! > > > > > > > > > > > Goodnight moon! > > > > > > > > > > > > > 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(); > > > > });
[jQuery] Re: Noob question about accessing element contents
$('item', xml).eq(1).text() - Richard On Wed, Jul 2, 2008 at 11:51 AM, ml1 <[EMAIL PROTECTED]> wrote: > > 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: > > > > > > > > > > > Hello world! > > > > > > > > Goodnight moon! > > > > > > > > > > > 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(); > > > > }); >
[jQuery] Re: Noob question about accessing element contents
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: > > > > > > > Hello world! > > > > > Goodnight moon! > > > > > > > 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(); > > });
[jQuery] Re: Noob question about accessing element contents
> 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: > > > > Hello world! > > Goodnight moon! > > > > 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(); });