Tom Gullen
Tom Gullen

Reputation: 61755

Jquery get HTML

I have the code:

// Get message
var PostTxt = $('#cid367', '.comment-txt').html();
PostTxt = $.trim(PostTxt);

It's trying to retrieve the comment from this structure:

<div id="cid367" class="comment-wrapper">  
    <div class="comment-head ch-highlight">        
        <div class="comment-date">    
            <abbr class="timeago" title="2011-04-25T15:15:52.4070000">25 April 2011 at 15:15:52</abbr>    
        </div>    
        <div class="comment-author">    
            Written by <a id="A1" title="Visit this game makers profile" href="../../../users/Tom">Tom</a>    
        </div>  
    </div>     
    <table class="comment-body" width="100%">    
        <tr>    
            <td width="100" valign="top" align="center">    
                <a id="A2" title="Tom makes games with Construct 2" href="../../../users/Tom"><img id="Img1" title="Tom&#39;s Gravatar" class="comment-avatar" src="http://www.gravatar.com/avatar/5271d3283db957ef3a86761ed15c1696?r=pg&amp;s=80" /></a>
            </td>    
            <td valign="top">    
                <div id="ModBox" class="comment-modbox" style="margin-left:-105px;">  
                    <a id="CommentReportPost" title="Report this post" class="s comment-report"></a>
                    <a id="CommentDeletePost" title="Delete this post" class="s comment-delete" onclick="DeleteComment(&#39;367&#39;);return false;" href="JavaScript:void(0)"></a>    
                    <a id="CommentEditPost" title="Edit this post" class="s comment-edit" onclick="EditComment(&#39;367&#39;);return false;" href="JavaScript:void(0)"></a>    
                    <a id="CommentQuotePost" title="Quote this post" class="s comment-quote" href="JavaScript:void(0)"></a>  
                </div>    
                <div class="comment-txt">    
                    My comment text to get    
                </div>    
            </td>    
        </tr>    
    </table>    
    <div class="clear"></div>    
</div>

But it keeps returning null. Can anyone show me how this is done?

Upvotes: 1

Views: 483

Answers (2)

neeebzz
neeebzz

Reputation: 11538

You are passing the parameters in the wrong sequence:

var PostTxt = $('#cid367', '.comment-txt').html();

It should be the other way round >

var PostTxt = $('.comment-txt', '#cid367').html();

Upvotes: 0

bendewey
bendewey

Reputation: 40245

You have it the wrong way around. It should be

var PostTxt = $('.comment-txt', '#cid367').html();

Where the first argument is the child and the second argument is the parent container.

$(childSelector, parentSelector)

When dealing with selectors like this is often helpful to check the length to ensure the selector is working before struggling with the html method

// for debugging
alert($('.comment-txt', '#cid367').length);  // if == 1 you're good

Upvotes: 5

Related Questions