Detecting <enter> key-press not working - please help

A

almurph

Hi everyone,


I'm a newbie to javascript. I have written some code to detect when a
user presses the down-arrow, up-arrow and enter button. Essentially
the user can arrow down or up through a list and the textbox is
populated with the option when the user presses the enter button. This
is for a VB.NET Web form type project.

The problem is that when I press the enter button the entire form get
refreshed and the javascript code is not executed. Or perhaps it is
but get "over-ruled" if you take my meaning.

Any comments/suggestions much appraciated. I have included the HTML
et al code below:

Cheers,
Al


****CODE*****


<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="WebForm1.aspx.vb" Inherits="WebApplication2.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script type="text/javascript">
function Test()
{

var retVal =
WebForm1.GetUsernameList(document.Form1.txtName.value);
//Populate the innerHTML of the div with the results

javascript:document.getElementById('nameList').style.display = "";
document.getElementById('nameList').innerHTML = retVal.value;

//alert(retVal.value)
}
</script>
<script type="text/javascript">
var count = 1

function previousSuggestion(){

var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')

items(count).style.backgroundColor = "#EFEFEF";

}//end previousSuggestion

function nextSuggestion(count){

var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')

items(count).style.backgroundColor = "#EFEFEF";

}//end nextSuggestion

function chooseSuggestion(){

document.getElementById ('txtName').innerText =
items(count).innerText

}//end chooseSuggestions

function keyHandler() {
switch (event.keyCode) {
case 38: //up-arrow
this.previousSuggestion()
if (count == 1)
count = count
else
count--
break
case 40: //down-arrow
this.nextSuggestion(count)
count++
break
case 13: //enter=13, tab=9
this.chooseSuggestion()
}
}
</script>
</HEAD>
<body ms_positioning="GridLayout" >
<form id="Form1" method="post" runat="server" onkeyup="javascript:
keyHandler()">
&nbsp; <INPUT id="txtName" onkeyup="javascript: Test()"
type="text">
<DIV id="nameList">&nbsp;</DIV>
</form>
</TEXTAREA></body>
</HTML>
 
W

wisestpotato

Try amending the keyHandler() function as below, so the section
relating to the enter key returns false. Returning false from a
keypress should stop any of the other default events associated with
that keypress.

function keyHandler() {
switch (event.keyCode) {
case 38: //up-arrow
this.previousSuggestion();
if (count == 1)
count = count;
else
count--;
break;
case 40: //down-arrow
this.nextSuggestion(count);
count++;
break;
case 13: //enter=13, tab=9
this.chooseSuggestion();
return false;
}
}
 
A

almurph

Try amending the keyHandler() function as below, so the section
relating to the enter key returns false. Returning false from a
keypress should stop any of the other default events associated with
that keypress.

function keyHandler() {
switch (event.keyCode) {
case 38: //up-arrow
this.previousSuggestion();
if (count == 1)
count = count;
else
count--;
break;
case 40: //down-arrow
this.nextSuggestion(count);
count++;
break;
case 13: //enter=13, tab=9
this.chooseSuggestion();
return false;
}



}- Hide quoted text -

- Show quoted text -

No joy wisepotato - I've tried it in both debug and release mode but
its not working. It does not throw and error but does not populate the
field. Bummer...
 
A

ASM

(e-mail address removed) a écrit :
Any comments/suggestions much appraciated. I have included the HTML
et al code below:

(snip)
function previousSuggestion(){
var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')

items(count).style.backgroundColor = "#EFEFEF";

items[count].style.backgroundColor = "#EFEFEF";
}//end previousSuggestion

function nextSuggestion(count){

var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')

items(count).style.backgroundColor = "#EFEFEF";

items[count].style.backgroundColor = "#EFEFEF";
}//end nextSuggestion

function chooseSuggestion(){

document.getElementById ('txtName').innerText =
items(count).innerText

always same error :

items[count] would be OK
 
A

almurph

(e-mail address removed) a écrit :


Any comments/suggestions much appraciated. I have included the HTML
et al code below:

(snip)
function previousSuggestion(){


var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')
items(count).style.backgroundColor = "#EFEFEF";

items[count].style.backgroundColor = "#EFEFEF";
}//end previousSuggestion
function nextSuggestion(count){
var Nodes = document.getElementById ('colm')
var items = Nodes.getElementsByTagName('td')
items(count).style.backgroundColor = "#EFEFEF";

items[count].style.backgroundColor = "#EFEFEF";
}//end nextSuggestion
function chooseSuggestion(){
document.getElementById ('txtName').innerText =
items(count).innerText

always same error :

items[count] would be OK


}//end chooseSuggestions

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Contact :http://stephane.moriaux.perso.wanadoo.fr/contact
ASM = Aimable Stéphane Moriaux = Amateur Sasseur Merdouilles

Thanks Stephen. I've implemented that - nice. Enter button still
clears everything though. I'm thinking now that this may be a VB.NET
"thing" rather than a "javascript" thing...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top