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

Updated 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