jQuery display text nested in paragraphs inside a div

Posted by user961627 on Stack Overflow See other posts from Stack Overflow or by user961627
Published on 2012-12-12T10:56:46Z Indexed on 2012/12/12 11:03 UTC
Read the original article Hit count: 217

Filed under:
|
|
|

I have HTML that looks something like this:

<div class='textbox' data-title='Sometitle'>
<div class='textareaedit'>
     <p><strong>Test sample text</strong></p>
</div>
</div>

I'm sometimes going to have a few different <p> tags inside the textareaedit divs, and sometimes strong tags around the text inside the <p> (as in this example), and sometimes a span tag, and sometimes it's going to be without further tags inside the <p>.

I want to iterate through each textbox on my page, grab its title and also the text nested inside <p> tags in textareaedit. I'm giving the output via console for testing. This is my jQuery code, but I get no output for the second console.log() line:

$('.textbox').each(function() {
    $this = $(this);
    console.log($this.attr('data-title')+ ":\n");
    $this.children('textareadit').children('p').each(function(){
        console.log($(this).html()); // not giving any output, it's blank
    });
});

I tried $(this).text() as well, but no difference. You may think this example has the sample text inside <strong> tags within the <p>, but I've also tried the same example without the strong, where the text was the direct child of <p>, but it didn't make a difference. How can I capture the text?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html