Malasorte Malasorte - 6 months ago 58
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

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();
});
Comments