[Solved] Attach to click event outside page


What you’re executing is a function, at the very beginning:

 $('#btnHello').click(function () {
    alert('Hi there');
 });

You’re hooking up your alert function to the element #btnHello.

But at the time it executes, #btnHello hasn’t been loaded. It doesn’t exist yet. So nothing gets hooked up.

You can get around this either by executing your code at the very end, or (more reliably, I think) deferring this code’s execution until the document has finished loading:

$(function () {
    $('#btnHello').click(function () {
       alert('Hi there');
     });
 });

You would put this in your global.js, but it wouldn’t execute until your page is completely loaded.

solved Attach to click event outside page