What is the reason that jQuery click does not work sometime?

author details
AdiPie
4th Aug 2021
2 mins read
Image
What is the reason that jQuery click does not work sometime

As jQuery documentation expresses: A page can't be controlled securely until the document is ready. Code included inside $( document).ready() will just run once the page Document Object Model (DOM) is prepared for JavaScript code to execute.

Event Handler

In case selector is discarded or is invalid, the event handler is assigned to direct bound. The handler is considered each time an occasion happens on the selected components, regardless of whether it happens straightforwardly on the component or bubble from a internal component.

Event handlers are bound uniquely to the presently selected elements; they should exist at the time your code settles the call to .on(). To guarantee the components are available and can be chosen, place scripts after the elements in the HTML markup or process of event binding a handler inside a document ready. Out of this use delegated event handlers to attach event handlers.

If we use the below code, sometimes it is not working, conflicting, or giving console error.

$('#clickid').click(function() { alert("hello"); });

The above code does not work sometimes. when we fire an event it does not show any alert and Console error.

$(document).on('click', '#clickid', function() { alert("hello"); });

If we have to check the above code then it will work properly. So, try the above code and it will not conflict with the jquery other events.