I've created a function that I can't get to return with onclick.
I'm sure it's something v simple i'm missing.
HTML:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="schedule.js"></script>
</head>
<body>
<input type="button" onclick="computeSchedule();" value="Submit" /><br>
<p id="scheduleOutput"></p>
</body>
</html>
JS:
function computeSchedule(loan_amount, interest_rate, payments_per_year, years, payment) {
var schedule = [];
var remaining = loan_amount;
var number_of_payments = payments_per_year * years;
for (var i=0; i<=number_of_payments; i++) {
var interest = remaining * (interest_rate/100/payments_per_year);
var principle = (payment-interest);
var row = [i, principle>0?principle:0, interest>0?interest:0, remaining>0?remaining:0];
schedule.push(row);
remaining -= principle
}
return schedule;
}
var list = JSON.stringify(computeSchedule(100000, 0.005, 12, 15, 843.86), 0, 4)
document.getElementById('scheduleOutput').innerHTML = list;
I can get the function to automatically return in jsFiddle onload, but when I change js settings to "no wrap - in head"it doesn't work. I assume whatever is happening there is also happening when I try to run outside of jsFiddle?
scripttag lines to the bottom of your HTML, just before the closingbodytag.schedule.jsfile, JavaScript code runs immediately, and at this point the HTML is not parsed as the DOM yet (which is why you getundefinedas a result ofdocument.getElementById). Try moving the loading ofschedule.jsto the bottom of the<body>tag (right before</body>). This is exactly as choosing "no wrap, in <body>" in JS Fiddle.