1

I have this HTML:

<p></p>    
<div class="comment_like">
    <span class="reaction_2 tooltipstered" id="like13seperator2" rel="unlike"><i 
    class="likeIconDefault"></i>Like</span>
</div>

Now I want to add this div: <div class="commentLikeCount"></div> before this comment_like class using jQuery.

I am trying with this code:

$("#like"+posts_id).parents(".comment_like").prepend('<div class="commentLikeCount"></div>');

but somehow not working :(

Updated for the new Questions:

Now I have that HTML:

<p></p>
<div class="commentLikeCount">
    <br>
    <span class="float-right">&nbsp;&nbsp;1</span>
    <img src="assets/images/db_haha.png" alt="" class="float-right old">
    <img src="assets/images/db_love.png" alt="" class="float-right">
</div>

<div class="comment_like">
    <span class="unLike_2" id="like13seperator2" rel="unlike">
        <i class="loveIconSmall likeTypeSmall"></i>Love
    </span>
</div>

Now, I just want to remove the last Img from the coomentLikeCount class.

1
  • Oh, I think I need to use before() :) Commented Jun 15, 2019 at 5:43

2 Answers 2

3

You can use insertBefore:

$('<div class="commentLikeCount" />')
.insertBefore($("#like"+posts_id).parents(".comment_like"))

Or before:

$("#like"+posts_id).parents(".comment_like")
.before('<div class="commentLikeCount" />')

If you're inserting commentLikeCount in every .comment_like, then just use $('.comment_like') instead of $("#like"+posts_id).parents(".comment_like")

Regarding your comment:

well if I already have this div then how can select this div?

You can prepend using insertBefore like:

$('.commentLikeComment').insertBefore($("#like"+posts_id).parents(".comment_like"));

To your updated question, you can remove last image like:

$('.commentLikeCount img').last().remove()
Sign up to request clarification or add additional context in comments.

7 Comments

well if I already have this div <div class="commentLikeCount"></div> then how can select this div?
Updated my answer.
Please check my updated questions: can use your updated code to get this?
should I use this also $("#like"+posts_id) ?
Yeah, if that's specific.
|
0

You can do with before().

$(".comment_like").before("<div class='commentLikeCount'></div>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.