Atadj
Atadj

Reputation: 7180

Submit form using JS hotkey?

This is my current code:

<select tabindex="2" id="resolvedformsel" name="resolved">
     <option selected="selected" value="yes">resolved</option>
     <option value="no">not resolved</option>
     <option value="mu">not a support question</option>
</select>
<input type="submit" value="Change" id="resolvedformsub" name="submit">

I want to use key combination (like Ctrl+Alt+1) to make select "resolved" and do "submit" at once. I'm doing this modification for support forum and it would be convenient to have hotkey to mark threads resolved.

I don't include jQuery! It has to be pure JS solution. Any ideas?

In other words: what is JS equivalent of jQuery's keypress/keydown?

Upvotes: 0

Views: 476

Answers (2)

micnic
micnic

Reputation: 11245

It would be better if you had a form, but you still can use:

window.addEventListener('keypress', function (event) {
    if (event.which == 13 && event.ctrlKey) { // Ctrl + Enter
        document.getElementById('resolvedformsel').options[0].selectected = true;
        document.getElementById('resolvedformsub').click();
    }
})

Upvotes: 2

Bernd
Bernd

Reputation: 1111

Key combinations are not easy and some keys are reserved. So it might not work. I would try it that way: Check in the jQuery source how they are catching the metaKey property of the event object. Take a look at some plugins handling events with key combinations. Hopefully this gives you some "inspiration".

Upvotes: 0

Related Questions