I have a text box, a search button and a huge amount of text in a page. What I want to do is, I want to type a string in the text box and click on search button. When i do so, I want all the results highlighted in that page. How do I achieve this?
Here is the POC of my code-
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Auto Completion</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<%
List<String> strList = new ArrayList<String>();
strList.add("\"red\"");
strList.add("\"orange\"");
strList.add("\"blue\"");
strList.add("\"pink\"");
strList.add("\"brown\"");
strList.add("\"yellow\"");
strList.add("\"violet\"");
strList.add("\"indigo\"");
strList.add("\"green\"");
strList.add("\"grey\"");
strList.add("\"black\"");
%>
<script>
$(function() {
var availableTags = <%=strList.toString() %>;
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
<script>
function selected () {
// What to do?
}
</script>
</head>
<body>
<%=strList.toString()%>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
<input type="button" onclick="javascript:selected();" value="Search"/>
</div>
</body>
</html>
Use a regexp object: http://jsfiddle.net/DQqLs/
You can then pass in the string from the text box, and it will highlight the matches with a span tag.