My <script></script>
loads on startup with the onchange function ( onchangeFunction() ) with it.
That means without the onchange event (selecting from drop down form) actually took place.
This means the function accessing null/unavailable variables end raising exception.
HTML
<td align="center" valign="top" class="reservation">
<form name="showSelector">
<h2 style="font-family:arial"> הזמנת כרטיסים: </h2>
<select name="Theater" class="selectFirst" onchange="theaterChange()">
<option value="999">theater...</option>
<c:forEach var="theater" items="${theaters}" varStatus="iter">
<option value="${theaters[iter.index].id}">"${theaters[iter.index].name}"
</option>
</c:forEach>
</select>
<select name="Movie">
<option>movie...</option>
</select>
<p></p>
<input type="submit" value="continue">
</form>
</td>
JavaScript
var group = new Array()
var temp = document.showSelector.Movie
var selectedTheater = -1;
function theaterChange() {
selectedTheater = document.showSelector.Theater;
var selcetedTheaterId = selectedTheater.options[selectedTheater.selectedIndex].value;
var selcetedTheaterName = selectedTheater.options[selectedTheater.selectedIndex].text;
if (1 == 555) { //this is trying to avoid the function content, but debugger continues as if condition is true
<% for (int i = 0; i < showIds.length; i++) { % >
if ( <%= showAtTheater[i] %> == selcetedTheaterId) { <% MovieIdInSelectedTheater.add(showAtTheater[i]);
idx = 0;
boolean found = false;
while (!found & idx < showAtTheater.length & idx < movieIds.length) {
if (showAtTheater[i] == movieIds[idx]) {
MovieNameInSelectedTheater.add(movieNames[idx]);
found = true;
} else {
idx++;
}
} %>
}
<% } %>
//...removed some function logic
temp.options[0].selected = true
}
}
servlet
out.write("<script>\n");
out.write("var group=new Array()\n");
out.write("\n");
out.write("var temp=document.showSelector.Movie\n");
out.write("var selectedTheater=-1;\n");
out.write("\n");
out.write("function theaterChange(){\n");
out.write("\n");
out.write("selectedTheater = document.showSelector.Theater;\n");
out.write("var selcetedTheaterId = selectedTheater.options[selectedTheater.selectedIndex].value;\n");
out.write("//var selcetedTheaterName = selectedTheater.options[selectedTheater.selectedIndex].text;\n");
out.write("\n");
MovieNameInSelectedTheater.clear();
out.write('\n');
MovieIdInSelectedTheater.clear();
out.write("\n");
out.write("\n");
out.write("\n");
for (int i=0;i<showIds.length;i++ ){
out.write("\n");
out.write(" if (");
out.print(showAtTheater[i]);
out.write(" == selcetedTheaterId)\n");
out.write(" {\n");
out.write(" ");
MovieIdInSelectedTheater.add(showAtTheater[i]);
idx = 0;
boolean found = false;
while (!found & idx<showAtTheater.length & idx<movieIds.length){
if (showAtTheater[i] == movieIds[idx] )
{
MovieNameInSelectedTheater.add(movieNames[idx]);
found = true;
}
else {idx++;}
}
out.write("\n");
out.write(" }\n");
out.write(" ");
}
out.write("\n");
out.write("\n");
out.write("\n");
for (int i=0;i<MovieNameInSelectedTheater.size();i++){
out.write("\n");
out.write(" temp.options[");
out.print(i);
out.write("]=new Option(\"");
out.print( MovieNameInSelectedTheater.get(i) );
out.write("\",\"");
out.print( MovieIdInSelectedTheater.get(i) );
out.write("\")\n");
}
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("\n");
out.write("temp.options[0].selected=true\n");
out.write("\n");
out.write("}\n");
out.write("\n");
out.write("</script>");
browser side:
<script>
var group=new Array()
var temp=document.showSelector.Movie
var selectedTheater=-1;
function theaterChange(){
selectedTheater = document.showSelector.Theater;
var selcetedTheaterId = selectedTheater.options[selectedTheater.selectedIndex].value;
//var selcetedTheaterName = selectedTheater.options[selectedTheater.selectedIndex].text;
if (2 == selcetedTheaterId)
{
}
temp.options[0].selected=true
}
</script>