Jump to content
Sign in to follow this  
Darkness

Key Presses

Recommended Posts

Is there a way to make it so, whenever a user presses a certain key (such as the down key), it submits a button?For example, I have this code:

<input type='submit' value='Submit'>

Whenever somebody presses the "down" key, I want it to submit. Is there a way to do this?Thanks!

Share this post


Link to post
Share on other sites

This is a javascript thing.First you need to capture the keystrokes and check for the down arrow. This link (just one of many) explains most of that: http://www.geekpedia.com/tutorial138_Get-k...JavaScript.htmlThen you need to submit the form. That would work something like this:document.getElementById("my_form").submit()But first, ask yourself REAL HARD if you really want to mess with the commonly-accepted user interface for submitting forms. Users are very resistant to change.

Share this post


Link to post
Share on other sites

If you use the submit button's value in your server side processing (and I imagine you do since you want to use a key that's not normally associated with submittion), you'll need to add a hidden form element with the same name and value before submittion.If you really want to do an "action" with the key instead of simply submitting the form (say, move something down), I suggest you try to do it in JavaScript (with or without AJAX) instead of submitting the form directly and refreshing/changing the page in the process.If you really want to submit a form (say, a search query), detect the enter key. As said already, users are very resistant to change, and if you try to force such a change, you'll likely just annoy your users.

Share this post


Link to post
Share on other sites

Well, it's not technically to submit a form. It's hard to explain.Is there a way to submit a link instead of a field? Like, when the user pressed "down", it would be as if they clicked on the link (it would bring them to http://blah.com);

<a href='http://blah.com'>Blah</a>

Image submit fields are inefficient, so I've decided to use a link instead of a submit field.Thanks.

Share this post


Link to post
Share on other sites

The easiest way is to alter the window.location property upon the proper key press, like so:

window.location = 'http://blah.com';

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...