2010/1/9 youradds <andy.ne...@gmail.com>

> Hi,
>
> Thanks. Ok, I have this string (for example):
>
> <html>
> <head></head>
> <body>
>
>
> bit of other junk here, and maybe other divs and stuff
>
>                        <div class="error">
>                                <ul>
>                                        <li>Gast Email der Rezension
> kann nicht den Wert 'undefined'
>                                        speichern.</li>
>                                </ul>
>                        </div>
>
> some junk here
>
> </body>
> </html>
>
> So how exactly would I extract that from a string?
>
> TIA :)
>
> Andy
>
>
> On Jan 9, 1:32 pm, Leonardo Balter <leonardo.bal...@gmail.com> wrote:
> > And you can simply use .text() method. This will return you only the text
> > inside your selected element.
> >
> > Do you also need tips on getting the child elements on that div?
> >
> > --
> > At,
> > Leo Balterhttp://leobalter.net
> > Blog técnico:http://blog.leobalter.net
>

Based on jquery documentation:

First, you can define your selectors, it's the easier way if you already
work with CSSs:

http://docs.jquery.com/Selectors

You can use something like: $('.error > ul > li').text(); to match all li's
inside the element with class set to 'error' (the . referees a element using
class name right after the dot).

You can also use $('.error ul li').text() to get "each" li descending from a
ul than from a element with a class named 'error'. In our .text() method
this won't make much diference than using the first selector.

Now let's try some jquery methods without complicating our basic selector:

We have the .find() and .children() methods

>From now I'm adapting the text from jquery documentation to our example:

"In most cases two selections made with and without find() are equivalent,
such as $('.error').find('li') and $('.error ul li'). However, using a
selector filter may lead to unexpected results:
$('.error').find('li:first').length may be > 1, (whereas $('.error
li:first').length will never be > 1) as there is an implicit each() done
within find()."

In other words: you can try to select the first li of all div with the class
attribute named 'error'.

Now the .children(), this method will return all the immediate descendants
elements within the selected element.

Example: $('.error').children('ul').children('li') this will return exactly
the directly children of the .error! .find() would return all descendants,
not only the immediate ones.

As said in the jquery documentation, it's important to refrain here: while
.children() returns only the immediate descendants, .parents() will look at
all ancestors.

That's all,

Have a good day.



-- 
At,
Leo Balter
http://leobalter.net
Blog técnico: http://blog.leobalter.net

Reply via email to