javascript - How to show suggestions for search -
i trying add suggestions search box. once user enters should show suggestions , if user hover on of suggestion should highlighted , when suggestion clicked should added search terms along semicolon separate search terms.
i able show suggestions problem implementing following
expected result following
search box >>> f suggestions first 4 when first clicked highlighted search box >>> first; when t clicked search box >>> first;t suggestions 2 3 click on 3 cause remove t search box , add 3 search box >>> first;three when tw entered search box >>> first;three;tw suggestions 2 twelve when 2 selected search box (first;three;two
suggestion.jsp
<c:foreach items="${sug}" var="mysug"> <label id="suggestion" onclick="selectsug(<c:out value="${mysug}"/>)"><c:out value="${mysug}"/></label> <br/> </c:foreach>
search.jsp
<script type="text/javascript"> function selectsug(value){ alert("vlue"+value); var temp = document.getelementbyid("mysearch").textcontent ; document.getelementbyid("mysearch").textcontent = temp + value + " ; "; } function findsug(value){ if(window.xmlhttprequest) { xmlhttp = new xmlhttprequest(); } else { xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if(xmlhttp.readystate == 4 && xmlhttp.status == 200) { document.getelementbyid("sugs").innerhtml=xmlhttp.responsetext; } } xmlhttp.open("get","search?input="+value,false); xmlhttp.send(); } ... <s:textfield id="mysearch" name="mysearch" label="search" onkeyup="findsug(this.value)"/> <div id="sugs"> </div>
mystyle.css
#suggestion:hover{ background:red; }
let me know if should include other part of code. wondering if can in easier way using jquery
try jquery-ui given widget
Comments
Post a Comment