Skip to main content

I am working a project which uses a function to show a modal dialog. The dialog can be hidehidden by calling the hideModal() function. This fuctionfunction is triggered by:

  • PressPressing the ESC key
  • ClickClicking on the modal background
  • ClickClicking on the close button

My current code is:

 $("#modal").click(function() {
    hideModal();
});

$("#modal-object-container > a").click(function(e) {
    hideModal();
    e.preventDefault();
});

$(document).keydown(function (e) {

    if (e.keyCode == '27') {
        hideModal();
    }

});

I have the feeling there should be a faster way to bind all these events at once.

I am working a project which uses a function to show a modal dialog. The dialog can be hide by calling the hideModal() function. This fuction is triggered by:

  • Press ESC key
  • Click on the modal background
  • Click on the close button

My current code is:

 $("#modal").click(function() {
    hideModal();
});

$("#modal-object-container > a").click(function(e) {
    hideModal();
    e.preventDefault();
});

$(document).keydown(function (e) {

    if (e.keyCode == '27') {
        hideModal();
    }

});

I have the feeling there should be a faster way to bind all these events at once.

I am working a project which uses a function to show a modal dialog. The dialog can be hidden by calling the hideModal() function. This function is triggered by:

  • Pressing the ESC key
  • Clicking on the modal background
  • Clicking on the close button

My current code is:

 $("#modal").click(function() {
    hideModal();
});

$("#modal-object-container > a").click(function(e) {
    hideModal();
    e.preventDefault();
});

$(document).keydown(function (e) {

    if (e.keyCode == '27') {
        hideModal();
    }

});

I have the feeling there should be a faster way to bind all these events at once.

Source Link

jQuery multiple events execute same function refactoring

I am working a project which uses a function to show a modal dialog. The dialog can be hide by calling the hideModal() function. This fuction is triggered by:

  • Press ESC key
  • Click on the modal background
  • Click on the close button

My current code is:

 $("#modal").click(function() {
    hideModal();
});

$("#modal-object-container > a").click(function(e) {
    hideModal();
    e.preventDefault();
});

$(document).keydown(function (e) {

    if (e.keyCode == '27') {
        hideModal();
    }

});

I have the feeling there should be a faster way to bind all these events at once.