The #
in #searchResult
means that you will get a single element that has the id
equal to searchResult
.
Solution No 1
I don't know where is your jsp I will assume that you have a getmovies.jsp
request then you can receive the results like thisjQuery
$.getJSON("getmovies.jsp", function(json) {
$.each (json, function(k, movie) {
console.log(movie.title);
});
});
Solution No. 2
If you have a code like this:
<% List moviesList = (List) session.getAttribute("clist");
JSONArray moviesJSON = new JSONArray();
JSONObject tmp;
for (int i = 0; i < selected.size(); i++) {
tmp = new JSONObject();
tmp.put("id", selected.get(i));
tmp.put("title", selected.get(i).getTitle());
moviesJSON.put(tmp);
}%>
Then you can receive the JSON in JavaScript like this:
var moviesJSON = <%=moviesJSON%>;
moviesJSON.each(function( movies ) {
movies.each(function(index, movie) {
console.log( index + ": " + movie.title );
});
NOTE This is another alternative that you can try~
I recommend you to use tags instead. Something like this $( "li" )
this expression will get the collection of DOM elements that have been selected from the document. In this case the tag li
.
This can be a solution for you.
var movies = $("#searchResult").find("li");
movies.each(function( index ) {
console.log( index + ": " + $( this ).text() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="searchResult">
These are the results:
<ul class="level-1">
<li class="result">I</li>
<li class="result">II</li>
<li class="result">III</li>
<li class="result">IV</li>
<li class="result">V</li>
</ul>
</div>