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

Discussion in 'Javascript' started by almurph@altavista.com, Mar 14, 2007.

  1. Guest

    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>
     
    , Mar 14, 2007
    #1
    1. Advertisements

  2. wisestpotato Guest

    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;
    }
    }
     
    wisestpotato, Mar 14, 2007
    #2
    1. Advertisements

  3. Guest

    On Mar 14, 4:36 pm, "wisestpotato" <>
    wrote:
    > 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...
     
    , Mar 14, 2007
    #3
  4. ASM Guest

    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
     
    ASM, Mar 14, 2007
    #4
  5. Guest

    On Mar 14, 4:51 pm, ASM <>
    wrote:
    > 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...
     
    , Mar 14, 2007
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Z. Hower aka Kudzu
    Replies:
    1
    Views:
    816
  2. Andrea Williams

    Key press "Enter" does not submit the form

    Andrea Williams, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    7,602
    Steve C. Orr [MVP, MCSD]
    May 24, 2004
  3. Jie
    Replies:
    7
    Views:
    6,691
    Xavier Pacheco
    Oct 9, 2004
  4. thechaosengine

    A msater page and detecting an "Enter" press

    thechaosengine, Feb 8, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    =?Utf-8?B?RFdT?=
    Feb 8, 2006
  5. WJ
    Replies:
    1
    Views:
    4,066
    Adrienne
    Jan 19, 2005
  6. gnewsgroup
    Replies:
    0
    Views:
    609
    gnewsgroup
    Dec 27, 2007
  7. BrianDH

    Enter Key H*ll, control enter key submit button

    BrianDH, Jan 12, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    629
    Anders Matthiessen
    Jan 12, 2005
  8. M P
    Replies:
    1
    Views:
    816
Loading...