Search code examples
jqueryradio-buttonclickmouseup

Two "or" conditions for event?


Fiddle: http://jsfiddle.net/TKWzH/8/

The "Test Pop" is the current function of the item. "Test Pop 2" is what I would like my script to function as. However, the actual script has a lot more functions on the click execution than just closing the popup.

Is there a way to combine the on enter and on mouseup commands into one line?

$("#popup2 label").on('keyup || mouseup', function (event) {
    $('#popup2').css('display', 'none');
});

Something like that is what I'm looking for, but I want the key up to only function for enterkey keyup. As that is written, it happens for any key.

The reason I'm trying to do this is because I want to make the form keyboard accessible. As you can tell from the fiddle, if you navigate through Test Pop with the keyboard, you have to reopen the dialogue after you select a radio button.


Solution

  • Just use a space.

    $("#popup2 label").on('keyup mouseup', function (e) {
        if (e.type == 'keyup' && e.which != 13) return;
        $('#popup2').css('display', 'none');
    });
    

    From the docs:

     .on( events [, selector] [, data], handler(eventObject) )
    

    events One or more space-separated event types and optional namespaces, such as "click" or "keydown.myPlugin".