Tom
Tom

Reputation: 771

autocomplete in textbox inside same jsp page

I am modifying the content. Data is being shown inside <div> country, but how to autofill a textbox present in auto.jsp? I'm getting data from get.jsp and content of get.jsp is being shown on auto.jsp in country <div>.

auto.jsp

<%@page import="java.sql.*"%>
<html>
    <head>  
        <script language="javascript" type="text/javascript">  
            var xmlHttp  
            var xmlHttp
            function showState(str){ 
                if (typeof XMLHttpRequest != "undefined"){
                    xmlHttp= new XMLHttpRequest();
                }
                else if (window.ActiveXObject){
                    xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
                }
                if (xmlHttp==null){
                    alert ("Browser does not support XMLHTTP Request")
                    return
                } 
                var url="get.jsp";//goes to get.jsp
                url += "?count=" +document.getElementById("textbox1").value;
                url += "&secondVal="+document.getElementById("textbox2").value;
                xmlHttp.onreadystatechange = stateChange;
                xmlHttp.open("GET", url, true);
                xmlHttp.send(null);
            }
            function stateChange(){   
                if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
                    document.getElementById("country").innerHTML=xmlHttp.responseText;  
                }   
            }   
        </script>  
    </head>  
    <body>  
        <input id="textbox1" type="text" name="name" onkeyup="showState(this.value)"/> 
        <input id="textbox2" type="text" name="secondVal" onkeyup="showState(this.value)"/>
        <br>  
        <div id='country'>
            Here Iam getting data from get.jsp
        </div>  
        <input type="text"/> //how to fill up this text box based on previous textbox 1 and textbox 2(written just above)?
    </body> 
</html>

get.jsp

<%
    String s=request.getParameter("count");//Got first textbox value
    out.println(s);
    String secondVal=request.getParameter("secondVal");// Got second textbox value
    out.println(secondVal);
    request.setAttribute("s", s);
%>

<input type="text" value="<%=s%>"/>

Upvotes: 0

Views: 3216

Answers (1)

user800014
user800014

Reputation:

See your function stateChange(), you are changing the value of country there. So you can change this to apply the result to other element too, for example:

function stateChange(){   
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
    document.getElementById("country").innerHTML = xmlHttp.responseText;  
    document.getElementById("textbox2").value = xmlHttp.responseText;
  }   
}  

Upvotes: 1

Related Questions