0

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?

Atadj
  • 7,050
  • 19
  • 69
  • 94
  • [what have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Eliran Malka Jul 24 '12 at 07:56
  • I have no idea what function do I need to use. I could write that easily in jQuery (using keydown, keypress of whatever) but in JS... I'm asking for some idea. Some solutions include http://stackoverflow.com/questions/3618280/using-a-hotkey-to-submit-an-html-form but that's not for me. – Atadj Jul 24 '12 at 07:58

2 Answers2

2

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();
    }
})
micnic
  • 10,915
  • 5
  • 44
  • 55
  • Great! I think this will work and it's so simple. So far it gives me `document.getElementById("resolvedformsel").elements is undefined` but I think I can easily find solution :) – Atadj Jul 24 '12 at 08:14
  • If you have a form that contains these elements, use form.submit() instead of document.getElementById('resolvedformsub').click(); – micnic Jul 24 '12 at 08:18
  • It works with click, surprisingly it doesn't with `submit();`. The error: `document.getElementById("resolved").submit is not a function`. Form has an id of `#resolved`. I'm not JS guru so perhaps I did something wrong - I started straight with jQuery (without JS basics). I think I can google some solution at this point :) Thanks a lot for all your help! – Atadj Jul 24 '12 at 08:28
  • are you sure that the form has the id 'resolved'? all form elements have .submit() method, as I see it can have the id "resolvedform" – micnic Jul 24 '12 at 08:38
  • Yup: http://gyazo.com/3a3acb2b5d9b187b71818dc6dbe97cb1.png?1343118968 It's not a big problem since it works with `click();` but might be some interesting exception (?). I removed `action=""` on screenshot. – Atadj Jul 24 '12 at 08:40
  • strange behavior :) .click() will not create any exception, but it is not aesthetically used, .submit() usually does this thing, .click() just simulates it... – micnic Jul 24 '12 at 08:49
0

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

Bernd
  • 1,111
  • 2
  • 9
  • 7