Enable click event on appended element using jQuery

Updated at 19-Aug-2021, By samar

Enable click event on appended element using jQuery

In this session, we are going to try to solve the "Enable click event on appended element using jQuery" puzzle by using the computer language.

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.
  • JQuery call on click function on appended anchor element

    <!-- HTML -->
    <button type="button" id="addAnchorBtn">
     Add Anchor element
    </button>
    <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>
    <script>
    $('#addAnchorBtn').on('click', function(){
    	html = '<a id="anchor" href="javascript:void(0)"> Anchor  </a>';
        	$('#element').append(html);
    });
    $(document).on('click', '#anchor', function(e) {
        alert('anchor clicked');
        console.log(e);
    });
    </script>
    

Back to code snippet queries related jquery