1

I'm currently working on something which may be called reply to someone post under some kind of article.

Basically, what I want to achieve is when you click 'reply to post' (.comment-reply class) which is under the post - you grab it's author name from .comment-username div and you put it to the textarea. The problem is to target this div and grab its content which I want to put in variable and put later to the textarea. Cannot figure this targeting and grabbing. The basic structure of entry is like this:

<div class="comment-block comment-child">
    <div class="comment-info title-small text-bold">
        <div class="push-left">
            <span class="comment-username sm-fc">AUTHOR NAME is here</span>
            <span class="comment-time">some date</span>
        </div>
        <div class="push-right">
            <span class="comment-rating">some int value</span>
        </div>
        <div class="clear"></div>

    </div>
    <div class="comment-text">
        Sample text => Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </div>
    <div class="comment-functions">
        <span class="comment-reply title-small text-bold">reply to post</span>
        <span class="comment-rate comment-plus"> + </span>
        <span class="comment-rate comment-minus"> - </span>
    </div>  
</div>

Furthermore, on the same page will be displayed a lot of posts so I dont want to use IDs for every single post, instead of that I'm using classes which is more clear to me in that case.

I've been trying with $.prev(), $.closest() but no results.

Any suggestions?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Marcin
  • 994
  • 2
  • 11
  • 26

2 Answers2

4

You can use closest() to get the common .comment-block container and from there find() the .comment-username and retrieve the text(). Try this:

$('.comment-reply').click(function() {
    var username = $(this).closest('.comment-block').find('.comment-username').text();
    // do something with the username here...
});

Example fiddle

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

With parents(selector) you can:

 var name = $(this).parents('.comment-username').text();

Good Luck.

Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69