How do I make the enter key select a combobox value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sjarmy
    New Member
    • Feb 2008
    • 1

    How do I make the enter key select a combobox value?

    I am using javascript to make a dropdownlist act like a combobox and it is work well. The issue I'm having is when the user types in the combobox and finds the selection he wants, he has to use the mouse to make the actual selection. The user wants to have the option of just hitting the enter key instead of using the mouse. He also wants to use the tab key to move to the next field. I'm using onkeydown to call the javascript, and I know I need to check for key code == 13 (for the enter key), but then what? How do I translate it into a select? I tried using document.getEle mentById("<%=Di rector.ClientID %>").click(); but the combobox just hung. Please help, someone.

    <script language="javas cript">
    var searchID;
    var searchValue;

    function checkKey(oList, e) {
    // Re-initialize if the
    if(oList.id != searchID) {
    searchID = oList.id;
    searchValue = "";
    }

    var theKey;
    var binIE;
    // check for browser event model
    if(window.event ) { theKey = window.event.ke yCode; binIE = true; }
    else { theKey = e.which; binIE = false; }

    if(theKey == 8) { // check for backspace
    searchValue = ""; // clear the filter
    }
    else if(theKey == 13) { // check for enter
    return;
    }
    // check for alphanumeric keys and append to the search value
    else if((theKey >= 48 && theKey <= 57) || (theKey >= 65 && theKey <= 90) ||
    (theKey == 32) || (theKey >= 96 && theKey <= 105)) {
    searchValue += String.fromChar Code(theKey).to LowerCase();
    }
    else { return false; }
    // loop through the list to select the proper item
    if(searchValue != "") {
    for(var iCheck=0;iCheck <oList.options. length;iCheck++ ) {
    if(oList.option s[iCheck].text.toLowerCa se().indexOf(se archValue) == 0) {
    oList.options[iCheck].selected = true;
    return;
    }
    }
    }
    }
    </script>


    <td align="left" width="25%">
    <asp:DropDownLi st id="Director" onkeydown="chec kKey(this, event);return false;" runat="server"> </asp:DropDownLis t>
    </td>
  • nityaprashant
    New Member
    • Feb 2008
    • 19

    #2
    =============== ========Check in javascript===== ====
    function captureEnterKey ()
    {
    if(event.keyCod e == 13)
    {
    // alert('keycode= ' + event.keyCode);
    var a= document.getEle mentById("ctl00 _ContentPlaceHo lder1_UserContr olPanel1_btnGo" )
    a.focus();
    }
    }
    =============== =============== ===============


    =============== ===Code Behind========= ==============
    txtProductName. Attributes.Add( "onkeydown" , "if(event.w hich || event.keyCode){ if ((event.which == 13) ||(event.keyCod e == 13)){document.g etElementById(' " + btnGo.UniqueID + "').click();ret urnfalse;}} else {return true}; ")

    ddlCategoryName .Attributes.Add ("onkeydown" , "if(event.w hich || event.keyCode){ if ((event.which == 13) ||(event.keyCod e == 13)){document.g etElementById(' " + btnGo.UniqueID + "').click();ret urnfalse;}} else {return true}; ")

    ddlSubcategoryN ame.Attributes. Add("onkeydown" , "if(event.w hich || event.keyCode){ if ((event.which == 13) ||(event.keyCod e == 13)){document.g etElementById(' " + btnGo.UniqueID + "').click();ret urnfalse;}} else {return true}; ")

    here ddlcategoryname & ddlsubcategory name is drop down list

    =============== =============== ======

    Comment

    Working...