Search code examples
javascriptjqueryinsertafter

Add something after 2 elements using $(this)?


I would like to add something after the 2nd or 3d element of $(this). This is actually always a form, just to let you know. After this form there are some other elements and at the the end there is another form, where you can go back.

After this form i would like to add a message, if a comment has been created, but how can i do something like:

$(this).after.after("<p class='success'>The comment has been edited!</p>");

or something like where i can also use the name of the class the form has? Actually the form to go back has the name back.

I actually need to use $(this) because else the message is added for each comment.


Solution

  • To target the next element if it is a form use .next('form')

    $(this).next('form').after("<p class='success'>The comment has been edited!</p>");
    

    To target something after two or three elements you can use the current index and add to it, and then eq to get that sibling

    $(this).siblings().eq($(this).index() + 2)