teeth
teeth

Reputation: 225

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.

Upvotes: 2

Views: 2181

Answers (2)

josh3736
josh3736

Reputation: 144912

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".

Upvotes: 4

adeneo
adeneo

Reputation: 318182

$("#popup2 label").on('keyup mouseup', function (e) {
    if (e.type==='keyup') { //key was pressed
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) { //Enter was pressed
            //Do something
            $('#popup2').css('display', 'none');
        }
    }else{
        //mouseup was the trigger
        $('#popup2').css('display', 'none');
    }
});

could be simplified a lot, but just to show how it's done!

Upvotes: 0

Related Questions