
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 </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>
0
Random Code Snippet Queries: Jquery
- How to validate input type file size in jQuery for multiple records
- Show loading icon on click submit button in jQuery
- How to use and purpose of localstorage in javascript
- Jquery.validate.min.js:4 Uncaught TypeError: Cannot read properties of undefined (reading 'mode')
- How to get closest form input value in jQuery
- Add and remove input fields on click button using jQuery
- How to remove Choose File button in jquery
- How to check ajax request is completed in jQuery
- Uncaught TypeError: $.ajaxSetup is not a function
- How to create read more in jquery
- Append method to add text to body not working
- Ajax done fail ajaxcomplete in not a function
- Preview image in BLOB URL format on choose file in jQuery
- Get the href link on click anchor in javascript
- Ajax for multiple form
- Show and hide target element on click button using javascript/jquery
- Mixed Content: The page at 'https://w3codegenerator.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
- How to pass javascript variable to laravel route on ajax call
- JQuery find text and show as bold in html page
- Preview image in base64 format using jQuery on change input file
- How to find closest upper element and append html to below it in jquery
- Execute function after Ajax call is complete
- How to reset form input element on click
- How to check object is empty or not in jQuery
- JQuery append html to below the target element