Click here to Skip to main content
15,937,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have write jquery code for Auto complete on text-box with onkeypress event it is working proper while text-box is empty but when i use ctrl+a or shift+tab and type to search some thing it is appending old data which resulting wrong search

e.g. if i search "1" in text box it showing list of all records with 1 but suppose i had pressed ctrl+a or shift+tab and enter "2" it is showing list of "12" instead of "2"

What I have tried:

<asp:TextBox ID="txtDrg" onkeypress="SearchText('DRG',this,event);" runat="server" ></asp:TextBox>

function SearchText(searchType, searchKey, event) {
if (searchType != '' && searchKey != '') {
    var x = event.which || event.keyCode;
    var searchKeyWord = '';

    if (searchType == 'DRG') {

        if (x == 8 || x == 46) {
            if (searchKey.value.length > 0) {
                searchKeyWord = searchKey.value.toString().slice(0, -1);
            }
        }
        else {
            if (x == 110 || x == 190)
                searchKeyWord = '.';
            else
                searchKeyWord = String.fromCharCode(x);
            if (searchKey.value != '' || searchKey.value != undefined)
                searchKeyWord = searchKey.value + searchKeyWord;
        }
        if (valiadte)
            GetAutoData(searchType, searchKeyWord);
    }
}
}
function GetAutoData() {
   //code 
}
Posted
Comments
Sinisa Hajnal 18-Aug-17 3:52am    
Exclude control keys from your event so it doesn't call SearchText when you press Ctrl, Tab, Alt or whatever. Might help. I admit I'm not an expert on html events, if onkeypress already ignores those, ignore this comment :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900