Richard
Richard

Reputation: 733

how to find the next span with a class not nested in jquery

I have the following code:

                    <div class="panel-heading">
                      {{$comment->user->name}} said <small class="text-primary">{{$comment->updated_at->diffForHumans()}}</small>
                      @if($comment->user_id == Auth::user()->id || Auth::user()->id == 2)
                      <a id="{{ $comment->id }}" class="pull-right comment_edit"><span class="glyphicon glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
                      <a id="{{ $comment->id }}" style="margin-right: 10px;" class="pull-right comment_delete"><span class="glyphicon glyphicon glyphicon-trash" aria-hidden="true"></span></a>
                      @endif
                    </div>
                    <div class="panel-body">
                      <span class="comment_textarea">{{$comment->comment}}</span>
                    </div>
                  </div>

When the button edit is clicked, I would like to get the text in the span with class "comment_textarea".

I tried with jquery to use:

$(document).on('click', '.comment_edit', function () {
 comment_comment = $(this).closest("comment_textarea").text();
 console.log(comment_comment);
});

But it doesn't return me anything. I tried also with next but it is the same.

I guess this is because it is in another div than where the button sits so how can I get the value from the next span with this class when it is not nested?

Upvotes: 0

Views: 22

Answers (1)

Khalid Khan
Khalid Khan

Reputation: 3185

Try next with find function Let me know if it helped you or not

$(document).on('click', '.comment_edit', function () {
 comment_comment = $(this).parent().next().find(".comment_textarea").text();
 console.log(comment_comment);
});

Upvotes: 1

Related Questions