Search code examples
javascriptjqueryclickhref

jquery click a link


I have an element that has a href attribute calling a javascript function. This element is in two classes : ( class="icon32 icon-close" ). This link is represented as an "X" on a div that has the class .modal and is used to close the div. My question is, can I make the div close, therefore, calling the a link, when the user presses the "esc" key. I tried the code below, but it does NOT work, although the alert shows up:

$(document).keypress(function (e) {
    if (e.keyCode == 27) {
        alert('esc was pressed');

        $('.modal>.icon32').click();
    }
});

Solution

  • You should be calling the .click() event of the native DOM element not the jQuery .click() event. To do that, have such code instead:

    $('.modal>.icon32')[0].click();