0

Can anyone give example for both click and on click event in jquery? I tried this example for on click, but the same function is happening for a click event also,

 <!DOCTYPE html>
 <html>
 <head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> 
 </script>
 <script>
 $(document).ready(function(){
 $("#afazer").on("click", "li", function() {
 $("#afazer").append("<li>Afazer 5</li>");
 $(this).remove();
 });
 });
 </script>
 </head>
 <body>
<ul id="afazer">
    <li>Afazer 1</li>
    <li>Afazer 2</li>
    <li>Afazer 3</li>
    <li>Afazer 4</li>
</ul>
</body>
</html>
2
  • what's exact issue? Dec 20, 2021 at 12:04
  • I referred to this code to a site they told that, If the event was associated with .click, job 5 will not obey the click event, and thus will not be removed. If it was associated via .on, with the separate selector, it will obey. but for me these conditions do not get satisfied..Can you give a difference between click and on click event with example.
    – Sweety
    Dec 20, 2021 at 12:16

1 Answer 1

3

.click events only work when an element gets rendered and are only attached to elements loaded when the DOM is ready.

.on events are dynamically attached to DOM elements, which is helpful when you want to attach an event to DOM elements that are rendered on an ajax request or something else (after the DOM is ready).

1
  • Thanks @Bhavin Gohil
    – Sweety
    Dec 20, 2021 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.