Hitting enter key should find text entered in text box.


Web tier: servlets, JSP, Web frameworks: Hitting enter key should find text entered in text box.

  1. Hi,

    I have a text box to enter customer code and if i hit enter key should call the below fn to find the entered customer code.

    function getKeyCode() {
     var keyCode = window.event.keyCode;
     if (keyCode == 13) {
       var code = document.findForm.searchCust.value;
       var url = '<%=request.getContextPath()%>';
       url = url+"/custDisp.do?search=true&custCode="+code;
       location.href = url1;
    <input type=text name=searchCust onKeyPress="javascript:getKeyCode();return true">

    But what happened means when i hit enter the url executed contains "http://custdev01:7001/PRO/customer/custDisp.do?searchCust=0280", instead of "http://custdev01:7001/PRO/customer/custDisp.do?search=true&custCode=0280". But the correct url is printed on alert box.

    what is wrong with script?
    Could someone help me to resolve this issue.

    Advance Thanks.
  2. I'd say you need to escape the '&' in your URL, ie instead of
    url = url+"/custDisp.do?search=true&custCode="+code;


    url = url+"/custDisp.do?search=true&amp;custCode="+code;
  3. Sorry Rich, it didn't work. Getting the same erroneous url executed. Is there anyother way, other than location.href, to execute the url.

  4. location.href = url1 but you are assigning url to the url you want to do to when enter is pressed. shouldn't the code read location.href = url;

    also try using the escape function in javascript when dealing with & and other special character.