How to find closest upper element and append html to below it in jquery

Created at 20-Aug-2021 , By samar

How to find closest upper element and append html to below it in jquery

Hello everyone, in this post we will look at how to solve "How to find closest upper element and append html to below it in jquery" in programming.

Sometimes you have to append the HTML content below or just after the target element. It will help you to find the closest upper element and append the HTML content below it in jQuery.
  • JQuery closest with after method

    <div id="targetElement" style="background-color:red;">
      <div id="element"> This is element </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script>
    $(document).ready(function(){
        html = '<p> Appended text to below (after) the target element  </p>';
    	$('#element').closest('#targetElement').after(html);
    })
    </script>
    

    This code snippet will help you to find the parent (upper) target element and add HTML content below (after) the target element.

Back to code snippet queries related jquery

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.