Liam
Liam

Reputation: 9855

jQuery function running on page load instead of on Click event

I have a function I've written that I want to only execute on the click on an element...

$(document).ready(function () {

$(".black-sectors li a.adr-src").on("click", updateAddressDisplay);

    updateAddressDisplay(null);
    function updateAddressDisplay(src) {
        # Function
    }

});

The above is running on the page load however?

Upvotes: 0

Views: 408

Answers (2)

George
George

Reputation: 36794

That'll be because you call it, right above where it's defined:

updateAddressDisplay(null); //Function called
function updateAddressDisplay(src) {
    # Function
}

Remove updateAddressDisplay(null); and it won't get called on page load.

Upvotes: 2

C M
C M

Reputation: 703

try removing updateAddressDisplay(null);

also you call pass some parameter with function on click call.

$(document).ready(function () {

$(".black-sectors li a.adr-src").on("click", updateAddressDisplay);

  //    updateAddressDisplay(null);
    function updateAddressDisplay(src) {
        # Function
    }

});

Upvotes: 0

Related Questions