-
Hi all,
Does anyone know if there is some javascript code that picks up on someone hitting the enter key?
What I mean is, I have an image map where someone can enter information into an empty field and hit enter and it automatically redirects to another page.
At the moment when someone hits enter the same page reloads. They need to click on a search button which is part of the image map to redirect them.
I would prefer for them to be able to simply hit and enter and get it to redirect.
Hope someone understands what I mean and has some code to do it :)
Thanking you in advance.
-
I am unsure of the exact syntax, but to refresh someone else, perhaps....
I remember something about an onKeyPress event that is fired. From there you can tell which key it is (using ASCII values, I believe).
Also, there are things such as 'hot keys' (just my terminology, no relevence really) that you can set up which will make your form fire when someone presses enter. I am unsure of the syntax.
Sorry I couldn't provide an accurate example, but please, everyone else post here! Thanks!
Hope this helps, however little! :)
-
Goober is quite right.
If you have a look at this script from DynamicDrive you'll see it's entirely possible. However, you need to find the correct ASCII number for hitting the 'Enter' key - something I'll leave to you ;)
The problem is, I'm not sure whether you can assign the Enter key, as it may conflict with other functions on the page (where you'd normally hit Enter).
Good luck....
-
Well, I do know from experience that when coding, chr(13) and chr(10) equal a new line (the equivalent of enter). Perhaps either one of these would work in your predicament. E-Mail me if you do not find it soon. I have an ASCII reference at home. (God bless the old IBM programming books, eh? ;))
Thanks for listenin' to me blab. :D
-
Hey guys,
Goober's right. To detect an enter, you use 13. Here's my example code:
Code:
<script>
function getKey(e){
keyCode = navigator.appName=="Netscape"?e.which:event.keyCode;
if(keyCode == 13){
alert('You pressed "Enter"');
}
}
if(document.layers)document.captureEvents(Event.KEYPRESS)
document.onkeypress = getKey;
</script>
aDog :cool:
P.S.-This new interface is weird...
-
Ahh, bingo! That's my thing learnt for the day.. Thanks aDog.
-
Sounds like a tech times tip!
Someone call Kevin! :D
-
Hey guys,
Here's actually the way I found that out (At first I thought it was 14, but wasn't sure...13's close to 14 :D).
Code:
<script>
function getKey(e){
keyCode = navigator.appName=="Netscape"?e.which:event.keyCode;
alert(keyCode);
}
if(document.layers)document.captureEvents(Event.KEYPRESS)
document.onkeypress = getKey;
</script>
Then, you can find any keycode, and sometimes they're different for IE and NN.
aDog :cool:
-
Thanks for all the help so far, but I still haven't managed to get the script finished (still relatively new to javascript).
At the moment when I enter something into the empty form field and press enter it still reloads the same page.
I'm not sure how to get one function to call another.
Here's the code I have so far:
Code:
<!-- this function displays a different div depending on the search string given -->
function chooser(){
if (document.form.name.value == "username1")
{
parent.location=username1.html';
}
if (document.form.name.value == "Username1")
{
parent.location=username1.html';
}
if (document.form.name.value == "")
{alert("You did not enter a User Name.")}
}
Can someone tell me how I incorporate the pressing of the nenter button into this script, so that when the enter is pressed the script recognizes it, then reads the content of the form field and redirects to the appropriate username1.html page.
Here's a link about "Javascript and your keyboard" that might help: http://www.wsabstract.com/javatutors...riptkey6.shtml
The enter key does use the number 13, btw.
Hope someone can help me out here :)
-
I got it working :D
The problem was that there was a form on the page also and the enter key was running the form instead of the javascript code, so I needed to add another function to tell it not to.
Thanks for all your help folks, it was really appreciated.