Change onLoad to onClick - javascript

So I'm creating a rescission calendar, and need to make some changes to the javascript, not really sure how.
All the code I create has to be inserted into a CMS system, which strips the head/body tag, so I can't use the onLoad function. What I want to have happen, is once they select a date, they click a submit button, and then the rescission date appears.
Here's the code from our previous system which didn't use a CMS:
Startscript language="javascript" type="text/javascript"<br>
hoyDate = new Date;<br>
closeDate = new Date;<br>
fundDate = new Date;<br>
resDate = new Date;<br>
monthsi = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');<br>
daysi = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');<br>
function setForm() {<br>
document.form.monthy.options.selectedIndex = closeDate.getMonth();<br>
document.form.datey.options.selectedIndex = closeDate.getDate() - 1;<br>
document.form.yeary.value = closeDate.getFullYear();<br>
}<br><br>
function calcRescission() {<br>
closeDate.setMonth(document.form.monthy.options[document.form.monthy.options.selectedIndex].value * 1);<br>
closeDate.setDate(document.form.datey.options[document.form.datey.options.selectedIndex].value * 1);<br>
closeDate.setFullYear(document.form.yeary.value * 1);<br>
<br>
resDate.setTime(closeDate.getTime());<br>
fundDate.setTime(closeDate.getTime());<br>
<br>
for (i=0; i<=2; i++) {<br>
resDate.setTime(resDate.getTime() + (3600000 * 24));<br>
//check for New Year's Day<br>
if (resDate.getMonth() == 0 && resDate.getDate() == 1) {<br>
i--;<br>
}<br>
//check for Christmas<br>
if (resDate.getMonth() == 11 && resDate.getDate() == 25) {<br>
i--;<br>
}<br>
//check for the 4th of July<br>
if (resDate.getMonth() == 6 && resDate.getDate() == 4) {<br>
i--;<br>
}<br>
//check for Veterans Day<br>
if (resDate.getMonth() == 10 && resDate.getDate() == 11) {<br>
i--;<br>
}<br>
//check for Memorial Day<br>
if (resDate.getMonth() == 4 && resDate.getDate() >= 25 && resDate.getDate() <= 31 && resDate.getDay() == 1) {<br>
i--;<br>
}<br>
//check for Labor Day<br>
if (resDate.getMonth() == 8 && resDate.getDate() >= 1 && resDate.getDate() <= 7 && resDate.getDay() == 1) {<br>
i--;<br>
}<br>
//check for Thanksgiving<br>
if (resDate.getMonth() == 10 && resDate.getDate() >= 22 && resDate.getDate() <= 28 && resDate.getDay() == 4) {<br>
i--;<br>
}<br>
//check for Martin Luther King, Jr observance<br>
if (resDate.getMonth() == 0 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
i--;<br>
}<br>
//check for Washington/Lincoln observance<br>
if (resDate.getMonth() == 1 && resDate.getDay() == 1 && resDate.getDate() >= 15 && resDate.getDate() <= 21) {<br>
i--;<br>
}<br>
//check for Columbus Day<br>
if (resDate.getMonth() == 9 && resDate.getDay() == 1 && resDate.getDate() >= 8 && resDate.getDate() <= 14) {<br>
i--;<br>
}<br>
//check for Sundays<br>
if (resDate.getDay() == 0) {<br>
i--;<br>
}<br>
}<br>
j = 0;<br>
fundit = "no";<br>
while (j<=3 || fundit != "yes") {<br>
fundit = "yes";<br>
j++;<br>
fundDate.setTime(fundDate.getTime() + (3600000 * 24));<br>
//checking for Christmas<br>
if ((fundDate.getMonth() == 11 && fundDate.getDate() == 25) || (fundDate.getMonth() == 11 && fundDate.getDate() == 26 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getMonth() == 11 && fundDate.getDate() == 25 && fundDate.getDay() == 0) {<br>
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for New Year's<br>
if ((fundDate.getMonth() == 0 && fundDate.getDate() == 1) || (fundDate.getMonth() == 0 && fundDate.getDate() == 2 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 1 && fundDate.getMonth() == 0 && fundDate.getDay() == 0) {
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for the 4th of July<br>
if ((fundDate.getMonth() == 6 && fundDate.getDate() == 4) || (fundDate.getMonth() == 6 && fundDate.getDate() == 5 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 4 && fundDate.getMonth() == 6 && fundDate.getDay() == 0) {
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//check for Memorial Day<br>
if (fundDate.getMonth() == 4 && fundDate.getDay() == 1 && fundDate.getDate() >= 25 && fundDate.getDate() <= 31) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Labor day<br>
if (fundDate.getMonth() == 8 && fundDate.getDay() == 1 && fundDate.getDate() >= 1 && fundDate.getDate() <= 7) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Thanksgiving<br>
if (fundDate.getMonth() == 10 && fundDate.getDay() == 4 && fundDate.getDate() >= 22 && fundDate.getDate() <= 28) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//check for Veterans Day<br>
if ((fundDate.getMonth() == 10 && fundDate.getDate() == 11) || (fundDate.getMonth() == 10 && fundDate.getDate() == 12 && fundDate.getDay() == 1)) {<br>
j--;<br>
if (fundDate.getDate() == 11 && fundDate.getMonth() == 10 && fundDate.getDay() == 0) {<br>
j++;<br>
}<br>
fundit = "no";<br>
}<br>
//checking for Martin Luther King, Jr observance<br>
if (fundDate.getMonth() == 0 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 21) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for Washington/Lincoln's Day<br>
if (fundDate.getMonth() == 1 && fundDate.getDay() == 1 && fundDate.getDate() >= 15 && fundDate.getDate() <= 22) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for Columbus Day<br>
if (fundDate.getMonth() == 9 && fundDate.getDay() == 1 && fundDate.getDate() >= 8 && fundDate.getDate() <= 14) {<br>
j--;<br>
fundit = "no";<br>
}<br>
//checking for weekends<br>
if (fundDate.getDay() == 6 || fundDate.getDay() == 0) {<br>
fundit = "no";<br>
if (fundDate.getDay() == 0) {<br>
j--;<br>
}<br>
}<br>
}<br>
//feed rescission end date to form<br>
// document.form.rescissionday.value = daysi[resDate.getDay()];<br>
document.form.rescissiondatey.value = resDate.getDate();<br>
document.form.rescissionmonthy.value = monthsi[resDate.getMonth()];<br>
document.form.rescissionyeary.value = resDate.getFullYear();<br>
//feed funding date to form<br>
document.form.fundday.value = daysi[fundDate.getDay()];<br>
document.form.funddatey.value = fundDate.getDate();<br>
document.form.fundmonthy.value = monthsi[fundDate.getMonth()];<br>
document.form.fundyeary.value = fundDate.getFullYear();<br>
document.form.nowday.value = daysi[closeDate.getDay()];<br>
}<br>
Endscript<br>
Here's the HTML:
<body onload="setForm();calcRescission();">
<form name="form" action="rescission.html" method="post">
<table cellpadding="4" cellspacing="0">
<tr>
<td>
<select name="monthy" onchange="calcRescission()" class="editable">
<option value="0">January</option>
<option value="1">February</option>
<option value="2">March</option>
</select>
<select name="datey" onchange="calcRescission()" class="editable">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input name="yeary" onkeyup="calcRescission()" size="7" class="editable">
</td>
</tr>
<tr>
<td>
<input name="rescissionmonthy" readonly size="10" class="rescind">
<input name="rescissiondatey" readonly size="3" class="rescind">
<input name="rescissionyeary" readonly size="7" class="rescind">
</td>
</tr>
</table>
</form>

You'll get rid of this:
<body onload="setForm();calcRescission();">
and add this to the script:
document.getElementsByTagName("body")[0].addEventListener("load", function() {
setForm();calcRescission()
}, false);

Related

how do I put my else/if return on a web page with user prompts for values

I have written the code below. I am asking for a user prompt for the values of day
and month and I want to return their star sign based on the values that they submit.
I do not know how to get the result of their input displayed on a web page.
So
user prompt for day
user prompt for month
then I want the starsign returned based on the else/if code. Can anybody help?
Thanks
var StarSign=
["Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius"
"Capricorn","Aquarius","Pisces"]
function signOftheZodiac(theDay,theMonth){
var theDay=prompt("What is your day of birth");
var theMonth=prompt("What is your month of birth");
if ((theDay>=21 && theMonth=="March") || (theDay<=19 && theMonth=="April")){
return StarSign[0];
}else if ((theDay>=20 && theMonth=="April")||(theDay<=20 && theMonth=="May")){
return StarSign[1];
}else if ((theDay>=21 && theMonth=="May")||(theDay<=20 && theMonth=="June")){
return StarSign[2];
}else if((theDay>=21 && theMonth=="June") ||(theDay<=22 && theMonth=="July")){
return StarSign[3]
}else if((theDay>=23 && theMonth=="July")||(theDay<=22 && theMonth=="August")){
return StarSign[4]
}else if((theDay>=23 && theMonth=="August")||(theDay<=22 && theMonth=="September")){
return StarSign[5]
}else if((theDay>=23 && theMonth=="September")||(theDay<=22 && theMonth=="October")){
return StarSign[6]
}else if((theDay>=23 && theMonth=="October")||(theDay<=21 && theMonth=="November")){
return StarSign[7]
}else if((theDay>=22 && theMonth=="November")||(theDay<=21 && theMonth=="December")){
return StarSign[8]
}else if((theDay>=22 && theMonth=="December")||(theDay<=19 && theMonth=="January")){
return StarSign[9]
}else if((theDay>=20 && theMonth=="January")||(theDay<=18 && theMonth=="February")){
return StarSign[10]
}else if((theDay>=19 && theMonth=="February")||(theDay<=20 && theMonth=="March")){
result= StarSign[11]
}
var h1=document.createElement("h1")
var textAnswer=document.createTextNode("Your Star sign is")
h1.setAttribute("id","signOftheZodiac");
h1.appendChild(textAnswer);
document.getElementById("flex-child").appendChild(h1)
}
function reset(){
document.getElementById("signOftheZodiac").remove();
}
I wouldn't want to do this with JS-Dialogs (which are ugly). Rather use HTML-Input that you can style (and optionally hide once you calculated the sign).
Something like this: (note that I fixed minor errors in your JS where you assigned result instead of returning it. Hope it's useable for your purposes ;)
var StarSign = ["Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius",
"Capricorn", "Aquarius", "Pisces"
];
function signOftheZodiac() {
var theDay = document.getElementById("day").value;
var d=document.getElementById("month");
var theMonth = d.options[d.selectedIndex].text;
;
console.log(theDay,theMonth);
if ((theDay >= 21 && theMonth == "March") || (theDay <= 19 && theMonth == "April")) {
return StarSign[0];
} else if ((theDay >= 20 && theMonth == "April") || (theDay <= 20 && theMonth == "May")) {
return StarSign[1];
} else if ((theDay >= 21 && theMonth == "May") || (theDay <= 20 && theMonth == "June")) {
return StarSign[2];
} else if ((theDay >= 21 && theMonth == "June") || (theDay <= 22 && theMonth == "July")) {
return StarSign[3]
} else if ((theDay >= 23 && theMonth == "July") || (theDay <= 22 && theMonth == "August")) {
return StarSign[4]
} else if ((theDay >= 23 && theMonth == "August") || (theDay <= 22 && theMonth == "September")) {
return StarSign[5]
} else if ((theDay >= 23 && theMonth == "September") || (theDay <= 22 && theMonth == "October")) {
return StarSign[6]
} else if ((theDay >= 23 && theMonth == "October") || (theDay <= 21 && theMonth == "November")) {
return StarSign[7]
} else if ((theDay >= 22 && theMonth == "November") || (theDay <= 21 && theMonth == "December")) {
return StarSign[8]
} else if ((theDay >= 22 && theMonth == "December") || (theDay <= 19 && theMonth == "January")) {
return StarSign[9]
} else if ((theDay >= 20 && theMonth == "January") || (theDay <= 18 && theMonth == "February")) {
return StarSign[10]
} else if ((theDay >= 19 && theMonth == "February") || (theDay <= 20 && theMonth == "March")) {
return StarSign[11]
}
}
function calc() {
var h1 = document.createElement("h1");
var textAnswer = document.createTextNode("Your Star sign is " + signOftheZodiac());
console.info(textAnswer);
h1.setAttribute("id", "signOftheZodiac");
h1.appendChild(textAnswer);
document.getElementById("flex-child").appendChild(h1)
}
function reset() {
document.getElementById("signOftheZodiac").remove();
}
<select id="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="number" id="day">
<button type="button" onclick="calc()">Calc</button>
<p id="flex-child"></p>

Can nested functions use variables with values set in other functions within the parent function? (Javascript)

I'm a total beginner trying to write code to roll on a series of branching d100 tables (the results of the first table determine which table is rolled on next, and so on).
I've gotten it to work by writing it all as one function, but because I'd like to loop a particular table several times while still referencing the results of the previous table, I'd like to do it as a series of nested functions.
The issue I'm running into is that if I nest the same code that worked as one function into a function within the parent function, EDIT: yearResults is now being written to the HTML but monthResults is coming back as undefined
Is there a way I can make this work? Is this a stupid way to go about trying to achieve this? Thank you for your help.
function fullRoll() {
var yearRoll
var yearResults
var monthRoll
var monthResults
// Rolls 1d100 on Yearly Events Table
function annualTable() {
var yearRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (yearRoll >= 1 && yearRoll <= 10) {
yearResults = "Ambassador";
} else if (yearRoll >= 11 && yearRoll <= 15) {
yearResults = "Assassination";
} else if (yearRoll >=16 && yearRoll <= 30) {
yearResults = "Birth";
} else if (yearRoll == 31) {
yearResults = "Comet";
} else if (yearRoll >=32 && yearRoll <=40) {
yearResults = "Death";
} else if (yearRoll >=41 && yearRoll <= 43) {
yearResults = "Earthquake";
} else if (yearRoll >=44 && yearRoll <= 47) {
yearResults = "Famine";
} else if (yearRoll >=48 && yearRoll <= 52) {
yearResults = "Fire";
} else if (yearRoll >=53 && yearRoll <= 57) {
yearResults = "Flood";
} else if (yearRoll == 58) {
yearResults = "Incursion";
} else if (yearRoll >= 59 && yearRoll <= 68) {
yearResults = "Marriage";
} else if (yearRoll == 69) {
yearResults = "Religion";
} else if (yearRoll >= 70 && yearRoll <= 74) {
yearResults = "Plague";
} else if (yearRoll >= 75 && yearRoll <= 78) {
yearResults = "Plot";
} else if (yearRoll >= 79 && yearRoll <= 87) {
yearResults = "Rebellion";
} else if (yearRoll == 88) {
yearResults = "Visitation";
} else if (yearRoll >= 89 && yearRoll <= 100) {
yearResults ="War";
}
}
// Determines the Results of a Roll on the Monthly Event Table
function monthlyTable() {
//Rolls on Monthly Table for Manmade Disasterss
if (yearResults == "Assassination" || yearResults == "Incursion" || yearResults == "Plot" || yearResults == "Rebellion" || yearResults == "War") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
if (monthRoll >= 1 && monthRoll <= 5) {
var monthResults = "Accident1";
} else if (monthRoll >= 6 && monthRoll <= 10) {
monthResults = "Bandits1";
} else if (monthRoll >=11 && monthRoll <= 15) {
monthResults = "Birth1";
} else if (monthRoll >= 16 && monthRoll <=20) {
monthResults = "Death1";
} else if (monthRoll >= 21 && monthRoll <=25) {
monthResults = "Taxes1";
} else if (monthRoll >=26 && monthRoll <=27) {
monthResults = "Fame1";
} else if (monthRoll == 28) {
monthResults = "Fire, Minor1";
} else if (monthRoll >= 29 && monthRoll <= 30) {
monthResults = "Haunting1";
} else if (monthRoll >=31 && monthRoll <=33) {
monthResults = "Monster1";
} else if (monthRoll >= 34 && monthRoll <=41) {
monthResults = "Incursion, Major1";
} else if (monthRoll >= 42 && monthRoll <= 51) {
monthResults = "Incursion, Minor1";
} else if (monthRoll >=52 && monthRoll <= 54) {
monthResults = "Injustice1";
} else if (monthRoll >= 55 && monthRoll <= 60) {
monthResults = "Battle, Major1";
} else if (monthRoll == 61) {
monthResults = "Criminal1";
} else if (monthRoll >=62 && monthRoll <= 66) {
monthResults = "Recruiting1";
} else if (monthRoll >=67 && monthRoll <=71) {
monthResults = "Troop Movements1";
} else if (monthRoll >=72 && monthRoll <= 78) {
monthResults = "Uprising1";
} else if (monthRoll >=79 && monthRoll <= 80) {
monthResults = "Vengeful Stranger1";
} else if (monthRoll >=83 && monthRoll <=100) {
monthResults = "Nothing1";
}
//Rolls on Table for Natural Disasters
} else if (yearResults == "Comet" || yearResults == "Earthquake" || yearResults == "Famine" || yearResults == "Fire" || yearResults == "Flood" || yearResults == "Plague") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (monthRoll >= 1 && monthRoll <= 5) {
monthResults = "Accident2";
} else if (monthRoll >= 6 && monthRoll <= 15) {
monthResults = "Bad Harvest2";
} else if (monthRoll >= 16 && monthRoll <= 22) {
monthResults = "Bad Weather2";
} else if (monthRoll >= 23 && monthRoll <= 32) {
monthResults = "Bandit Activity2";
} else if (monthRoll >= 33 && monthRoll <= 37) {
monthResults = "Birth2";
} else if (monthRoll >= 38 && monthRoll <= 44) {
monthResults = "Death2";
} else if (monthRoll >= 45 && monthRoll <= 49 ) {
monthResults = "Earthquake, Minor2";
} else if (monthRoll >= 50 && monthRoll <= 54) {
monthResults = "Taxes2";
} else if (monthRoll >= 55 && monthRoll <= 60) {
monthResults = "Fire, Minor2";
} else if (monthRoll >= 61 && monthRoll <= 66) {
monthResults = "Flooding2";
} else if (monthRoll >= 67 && monthRoll <= 69) {
monthResults = "Haunting2";
} else if (monthRoll >= 70 && monthRoll <= 73) {
monthResults = "Monster2";
} else if (monthRoll >= 74 && monthRoll <= 78) {
monthResults = "Injustice2";
} else if (monthRoll >= 79 && monthRoll <= 82) {
monthResults = "Landslide2";
} else if (monthRoll >= 83 && monthRoll <= 87) {
monthResults = "Maneater2";
} else if (monthRoll >= 88 && monthRoll <= 92) {
monthResults = "Plague2";
} else if (monthRoll == 93) {
monthResults = "Uprising2";
} else if (monthRoll == 94) {
monthResults = "VIP2";
} else if (monthRoll >= 95 && monthRoll <=100) {
monthResults = "Nothing2";
}
//Rolls on Table for Other Disasters
} else if (yearResults == "Ambassador" || yearResults == "Birth" | yearResults == "Death" || yearResults == "Marriage" || yearResults == "Visitation") {
monthRoll = Math.floor(Math.random()*(100-1+1)+1);
// Interprets the results of the roll
if (monthRoll >= 1 && monthRoll <= 5) {
var monthResults = "Accident3";
} else if (monthRoll >= 6 && monthRoll <= 7) {
monthResults = "Bad Harvest3";
} else if (monthRoll >= 8 && monthRoll <= 10) {
monthResults = "Bad Weather3";
} else if (monthRoll >= 11 && monthRoll <= 15) {
monthResults = "Bandit Activity3";
} else if (monthRoll >= 16 && monthRoll <= 25) {
monthResults = "Birth3";
} else if (monthRoll >= 26 && monthRoll <= 30) {
monthResults = "Death3";
} else if (monthRoll >= 31 && monthRoll <= 35) {
monthResults = "Famous Person3";
} else if (monthRoll >= 36 && monthRoll <= 40) {
monthResults = "Fire, Minor3";
} else if (monthRoll >= 41 && monthRoll <= 43) {
monthResults = "Haunting3";
} else if (monthRoll >= 44 && monthRoll <= 50) {
monthResults = "Injustice3";
} else if (monthRoll >= 51 && monthRoll <= 55) {
monthResults = "Work of Art3";
} else if (monthRoll >= 56 && monthRoll <= 60) {
monthResults = "Maneater3";
} else if (monthRoll >= 61 && monthRoll <= 68) {
monthResults = "Marriage3";
} else if (monthRoll >= 69 && monthRoll <= 78) {
monthResults = "Criminal3";
} else if (monthRoll >= 79 && monthRoll <= 83) {
monthResults = "Vengeful Stranger3";
} else if (monthRoll >= 84 && monthRoll <= 92) {
monthResults = "VIP Visit3";
} else if (monthRoll >= 93 && monthRoll <= 100) {
monthResults = "Nothing3";
}
}
}
annualTable();
monthlyTable();
document.getElementById('annual').innerHTML = yearResults;
document.getElementById('month1').innerHTML = monthResults;
}
Your conditions are wrong:
if (yearRoll >= 1 && <= 50) {
Should be:
if (yearRoll >= 1 && yearRoll <= 50) {
Remember to do that for all range checks in your code.
Moreover, considering the comment in your code is true (random number between 1 and 100), you could just go ahead and drop the >=1 and <=100 checks altogether.
Yes, nested functions will have access to variables declared in the parent function.
However, this is probably not what you want as it is very easy to overwrite one of these values accidentally.
Instead, consider breaking your other functions out as separate functions in their own scope. See the below example (with minor modifications to remove syntax errors):
function fullRoll() {
var yearResults = annualTable();
var monthResults = monthlyTable(yearResults);
document.getElementById('annual').innerHTML = yearResults;
document.getElementById('month1').innerHTML = monthResults;
}
function annualTable() {
var yearRoll = 23; //*random number between 1 and 100//*
if (yearRoll >= 1 && yearRoll <= 50) {
return "x";
} else if (yearRoll >= 51 && yearRoll <= 100) {
return "y";
}
}
function monthlyTable(yearResults) {
if (yearResults == "x") {
var monthRoll = 72; //*random number between 1 and 100//*
if (monthRoll >= 1 && monthRoll <= 100) {
return "a";
}
} else if (yearResults == "y") {
var monthRoll = 23; //*random number between 1 and 100//*
if (monthRoll >=1 && monthRoll <=100) {
return "b";
}
}
}

If condition does not follow (JavaScript)

Creating a SharePoint Portal using JavaScript and HTML where the problem is when i'm inputting a number around 100,000 to 800,000; ex(523,546) it would enter in the first if condition and do the statement below in the. even though the value is less than the given MDV "Total Estimated Freight".
The value of the pagetdv is inputted.
var pagetdvz = document.getElementById('pagetdv');
var pagesrz = document.getElementById('pagesr');
var pagemdvz = document.getElementById('pagemdv');
/------------ Maximum Declared Value ----------/
if (document.getElementById('dropct').selectedIndex == 0)
document.getElementById('pagemdv').value = 1500000;
else if (document.getElementById('dropct').selectedIndex == 1)
document.getElementById('pagemdv').value = 3000000;
/------------ Dest + 20 Ftr ----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 50000;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 47583.67;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59981.33;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59000;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 45900;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 59,981.33;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 58500;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 49000;
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 46700;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 0))
document.getElementById('pagesr').value = 51000;
/------------ Dest + 40 Ftr----------/
if ((document.getElementById('dropdest').selectedIndex == 0) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 1) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 95500;
else if ((document.getElementById('dropdest').selectedIndex == 2) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 3) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 94944.66;
else if ((document.getElementById('dropdest').selectedIndex == 4) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 5) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 6) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 117487.73;
else if ((document.getElementById('dropdest').selectedIndex == 7) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 89600;
else if ((document.getElementById('dropdest').selectedIndex == 8) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 119739.26;
else if ((document.getElementById('dropdest').selectedIndex == 9) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 113000;
else if ((document.getElementById('dropdest').selectedIndex == 10) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
else if ((document.getElementById('dropdest').selectedIndex == 11) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = 91096.87;
else if ((document.getElementById('dropdest').selectedIndex == 12) && (document.getElementById('dropct').selectedIndex == 1))
document.getElementById('pagesr').value = "--Null--";
/------------ Total Estimated Freight ----------/
if (document.getElementById('pagetdv').value > document.getElementById('pagemdv').value){
var tdva = parseFloat(pagetdvz.value) - parseFloat(pagemdvz.value);
var tdvb = tdva / 1000;
var tdvc = tdvb * 3.36;
var tef = tdvc + parseFloat(pagesrz.value);
document.getElementById('pagetef').value = tef;
}
else if (document.getElementById('pagetdv').value <= document.getElementById('pagemdv').value)
document.getElementById('pagetef').value = document.getElementById('pagesr').value;

It is supposed to increment wherever it finds a similar entry

for(var i= 0; i < foundRecords.length ; i++){
var MaleChildNew=0,
MaleNew=0,
femaleChildNew=0,
femaleNew=0,
policeMaleChildNew = 0,
policefemaleChildNew=0,
policeMaleNew=0,
policefemaleNew=0,
npoliceMaleChildNew=0,
npoliceMaleNew=0,
npolicefemaleChildNew=0,
npolicefemaleNew=0;
if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleNew++;
}
}
The problem is that it only increments the category which it finds in the end of the database and returns 1 for the same. What I want is that it should increment the value for every entry it finds similar and save the value. But this code returns 0 for all other variables and 1 for the one which is at the end of the DB.
Your code will always initialise the variables to 0 as they are inside the for loop. So, the variables are reset to 0 for each item of foundRecords. Thus, declare your variables outside the for loop:
var MaleChildNew=0,
MaleNew=0,
femaleChildNew=0,
femaleNew=0,
policeMaleChildNew = 0,
policefemaleChildNew=0,
policeMaleNew=0,
policefemaleNew=0,
npoliceMaleChildNew=0,
npoliceMaleNew=0,
npolicefemaleChildNew=0,
npolicefemaleNew=0;
for(var i= 0; i < foundRecords.length ; i++){
if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
policeMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
policefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
npoliceMaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
npolicefemaleNew++;
} if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleChildNew++;
}else if(foundRecords[i]['age'] <= 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleChildNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'male' && foundRecords[i]['opdType'] == 'new' ){
MaleNew++;
}else if(foundRecords[i]['age'] > 12 && foundRecords[i]['patientType'] == 'policePerson' && foundRecords[i]['sex'] == 'female' && foundRecords[i]['opdType'] == 'new' ){
femaleNew++;
}
}

Can't create href link in innerHTML statement

I have a function with a stream of if statements. If a statement is true, it writes back to the document using innerHTML. Within the innerHTML I can add p tags and text, but as soon as I add an <a href> line it give me an "Uncaught SyntaxError: Unexpected identifier" error. What am I doing wrong? The error is within the calage() function with the following snippet:
if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) {
console.log("They should be in the 2YO");
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> QSI Shekou!";
}
The full HTML file is below. Thank you for your help!
<!DOCTYPE HTML>
<html>
<head> </head>
<body>
<table cellpadding=0 cellspacing=0 style="width:95%;" align="center">
<tr><td align="center" class="abouttabletext"><br>
<script type="text/javascript">
var startyear = "1995";
var endyear = "2013";
var dat = new Date();
var curday = dat.getDate();
var curmon = dat.getMonth()+1;
var curyear = dat.getFullYear();
function checkleapyear(datea)
{
if(datea.getYear()%4 == 0)
{
if(datea.getYear()% 10 != 0)
{
return true;
}
else
{
if(datea.getYear()% 400 == 0)
return true;
else
return false;
}
}
return false;
}
function DaysInMonth(Y, M) {
with (new Date(Y, M, 1, 12)) {
setDate(0);
return getDate();
}
}
function datediff(date1, date2) {
var y1 = date1.getFullYear(), m1 = date1.getMonth(), d1 = date1.getDate(),
y2 = date2.getFullYear(), m2 = date2.getMonth(), d2 = date2.getDate();
if (d1 < d2) {
m1--;
d1 += DaysInMonth(y2, m2);
}
if (m1 < m2) {
y1--;
m1 += 12;
}
return [y1 - y2, m1 - m2, d1 - d2];
}
function calage()
{
var calday = document.birthday.day.options[document.birthday.day.selectedIndex].value;
var calmon = document.birthday.month.options[document.birthday.month.selectedIndex].value;
var calyear = document.birthday.year.options[document.birthday.year.selectedIndex].value;
if(curday =="" || curmon=="" || curyear=="" || calday=="" || calmon=="" || calyear=="")
{
alert("please fill all the values and click go -");
}
else
{
var curd = new Date(curyear,curmon-1,curday);
var cald = new Date(calyear,calmon-1,calday);
var diff = Date.UTC(curyear,curmon,curday,0,0,0) - Date.UTC(calyear,calmon,calday,0,0,0);
var dife = datediff(curd,cald);
var monleft = (dife[0]*12)+dife[1];
var secleft = diff/1000/60;
var hrsleft = secleft/60;
var daysleft = hrsleft/24;
var as = parseInt(calyear)+dife[0]+1;
var datee = diff/1000/60/60/24;
}
if((calyear == 2010 && calmon > 10) || (calyear == 2011 && calmon < 11)) {
console.log("They should be in the 2YO");
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> Visit W3Schools!";
}
if((calyear == 2009 && calmon > 10) || (calyear == 2010 && calmon < 11)) {
console.log("They should be in the 3YO");
return 3;
}
if((calyear == 2008 && calmon > 10) || (calyear == 2009 && calmon < 11)) {
console.log("They should be in the 4YO");
}
if((calyear == 2007 && calmon > 10) || (calyear == 2008 && calmon < 11)) {
console.log("They should be in the 5YO");
}
if((calyear == 2006 && calmon > 10) || (calyear == 2007 && calmon < 11)) {
console.log("They should be in the 6YO");
}
if((calyear == 2005 && calmon > 10) || (calyear == 2006 && calmon < 11)) {
console.log("They should be in the 7YO");
}
if((calyear == 2004 && calmon > 10) || (calyear == 2005 && calmon < 11)) {
console.log("They should be in the 8YO");
}
if((calyear == 2003 && calmon > 10) || (calyear == 2004 && calmon < 11)) {
console.log("They should be in the 9YO");
}
if((calyear == 2002 && calmon > 10) || (calyear == 2003 && calmon < 11)) {
console.log("They should be in the 10YO");
}
if((calyear == 2001 && calmon > 10) || (calyear == 2002 && calmon < 11)) {
console.log("They should be in the 11YO");
}
if((calyear == 2000 && calmon > 10) || (calyear == 2001 && calmon < 11)) {
console.log("They should be in the 12YO");
}
if((calyear == 1999 && calmon > 10) || (calyear == 2000 && calmon < 11)) {
console.log("They should be in the 13YO");
}
if((calyear == 1998 && calmon > 10) || (calyear == 1999 && calmon < 11)) {
console.log("They should be in the 14YO");
}
if((calyear == 1997 && calmon > 10) || (calyear == 1998 && calmon < 11)) {
console.log("They should be in the 15YO");
}
if((calyear == 1996 && calmon > 10) || (calyear == 1997 && calmon < 11)) {
console.log("They should be in the 16YO");
}
if((calyear == 1995 && calmon > 10) || (calyear == 1996 && calmon < 11)) {
console.log("They should be in the 17YO");
}
if(calyear == 1995 && calmon < 11) {
console.log("Your child is too old to attend QSI");
}
}
</script>
<form name="birthday" action="">
Date<select name="day" size="1">
<script type="text/javascript">
for(var j=1;j<32;j++)
document.write("<option value="+j+">"+j+"</option>");
</script></select>
Month<select name="month" size="1">
<script type="text/javascript">
for(var i=1;i<13;i++)
document.write("<option value="+i+">"+i+"</option>");
</script></select>
Year
<select name="year" size="1">
<script type="text/javascript">
for(var k=startyear;k<endyear;k++)
document.write("<option value="+k+">"+k+"</option>");
</script></select>
<br><br>
<input name="start" onclick="calage()" value="Calculate" type="button">
</form>
</td></tr></table>
<div id="demo">
</div>
</body>
</html>
try this:
document.getElementById("demo").innerHTML="<p>Your student would more than likely enter the 2YO class. The 2YO students will have class at the Nanshan campus. To apply, please fill out the following application.</p> <a href='http://qsishekou.org' target='_blank'>QSI Shekou!</a>";
enclose the string with "", like you have - so use ' to surround attributes.
for example:
var str = "hello, I am" gonna go" here";
that will generate an error because you close the string and then you don't concatenate, thus the exception.
so,
var str = "hello, I am' gonna go' here";
would be appropriate.
or
var str = "hello, I am" + "gonna go here";
or if you have to use double quotes, then backslash them.
var str = "hello, I am\" gonna go\" here";
In my case, I already used simple quotes as #jamesemanon suggests, but it still didn't work. In Android devices no problem, IOS simulator no problem, but IOS devices failed.
The solution for me was just remove the target="_blank" attr, and link work now on devices.

Categories