SOLVED: Stray } within the Javascript. Eclipse IDE doesn’t check these when in a JSP.
I’m having trouble getting my Javascript functions to be called in my JSP. I have commented above which functions are working and not working. My goal is to have my dropdown boxes to be preselected based on a passed parameter.
<script type="text/javascript">
//this alert not working
alert('alert1');
function preloadDropdownBoxes() {
//preload function alert not calling
alert('preload function called');
//mapping vars to java variables
var sYear = '<%=sYear%>';
var collectionPeriod = '<%=collectionPeriod%>';
var submission = '<%=submission%>';
var availDate = '<%=availDate%>';
var openDate = '<%=openDate%>';
var closeDate = '<%=closeDate%>';
//these are time generated values based on calendar instance
var pastTwoYear = '<%=pastTwoYear%>';
var pastYear = '<%=pastYear%>';
var currentYear = '<%=currentYear%>';
var futureYear = '<%=futureYear%>';
//change selectedIndex values if dropdown in JSP has "pastTwoYear"
if (sYear == pastTwoYear) {
document.getElementById('pastTwoYear').selectedIndex = 0;
} else if (sYear == pastYear) {
document.getElementById('past').selectedIndex = 0;
alert(sYear + '0');
} else if (sYear == currentYear) {
document.getElementById('current').selectedIndex = 1;
alert(sYear + '1');
} else if (sYear == futureYear) {
document.getElementById('future').selectedIndex = 2;
alert(sYear + '2');
} else if(sYear == futureTwoYear) {
document.getElementById('futureTwoYear').selectedIndex = 3;
alert(sYear + '3');
}
} //end preLoadDropdownBoxes()
</script>
I then have code for my dropdown boxes. Here is a snippet:
<select name="sYear1" id="sYear1" onchange="javascript:PreselectMyItem();">
<option value="past"><%=cal.get(java.util.Calendar.YEAR)-2%>-<%=cal.get(java.util.Calendar.YEAR)-1%></option>
<option value="current"><%=cal.get(java.util.Calendar.YEAR)-1%>-<%=cal.get(java.util.Calendar.YEAR)%></option>
<option value="future"><%=cal.get(java.util.Calendar.YEAR)%>-<%=cal.get(java.util.Calendar.YEAR)+1%></option>
</select>
Now, finally, I have another script on the bottom. I plan on putting my preloadDropdownBoxes(); function at the end. I tried used $(document).ready but it wasn’t working. Code is as follows:
<script>
// these two alert calls work
alert('footer');
alert('footer next');
// these functions aren't being called
window.preloadDropdownBoxes();
document.getElementById('future').selectedIndex = 2;
window.alert('document ready on bottom');
</script>
</body>
</html>
Any help would be greatly appreciated!
Edit: I’ve made my Test button as follows <input type="button" value="test" onclick="javascript:alert('test');"/> and this alert does work. There seems to be something wrong with the document not being able to register the functions in my script up top because I tried
function test() {
alert(‘hello’);
}
But the above snippet function isn’t called when the button is: <input type="button" value="test" onclick="javascript:test();"/>
There are two problems here
}at line number 125 (Just above the</head>tag at the end of commentsfutureTwoYearis not defined. You may have to definevar futureTwoYear= '2014-2015';Code: