Malasorte Malasorte - 1 year ago 131
Javascript Question

Combine onload, onresize and onclick

The function below runs on document load and document resize:

$(window).on("resize", function () {
// code
}).resize();


How can I also trigger the function when the user clicks on
<div id="my_div"></div>
?

Answer Source

Move your function to some named function and call it on event:

function myFunction () {
    alert('Working');
}

$(window).on("resize", function () {
    myFunction();
}).resize();

$('#my_div').click(function () {
    myFunction();
});