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

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.

Answers 1
  • 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>
    
    0

    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