Enable click event on appended element using jQuery

Click event has an issue on an appended element in jQuery. After appending the html element click event did not work on it. You can use $(document).on('click', '#anchor', function(e) { to enable click event on appended element in jQuery.

Answers 1
  • JQuery call on click function on appended anchor element

    <!-- HTML -->
    <button type="button" id="addAnchorBtn">
     Add Anchor element
    <div id="element"></div>
    <!-- JQuery CDN with javascript code -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    $('#addAnchorBtn').on('click', function(){
    	html = '<a id="anchor" href="javascript:void(0)"> Anchor  </a>';
    $(document).on('click', '#anchor', function(e) {
        alert('anchor clicked');
  • Back to code snippet queries related jquery