I am trying to write a simple javascript function to calculate the totals based on data printed out by php and it is giving me a Javascript Reference error which I have Googled, but the error is too simplistic and common for me to find any information specific to my problem.
I have clearly made the functions and I can not understand what I have done wrong. I know it probably just a formatting error but I can not see it.
The JSFiddle is here (it has static examples of what the php would print): JSFiddle Example
And my javascript:
function calculcateTotal(obj){
var id = obj;
var ticketCost = document.getElementById(obj).value;
var i = 1;
boolean firstTicket = true;
if(document.getElementById('term1').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term2').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term3').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term4').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}
var runningTotal = parseFloat(ticketCost)*parseFloat(i);
document.getElementById('total').value = runningTotal;
}
function changeTotal(){
var ticketCost = document.getElementById('total').value;
var i = 1;
boolean firstTicket = true;
if(document.getElementById('term1').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term2').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term3').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}else if(document.getElementById('term4').selected == true){
if(firstTicket == false){
i = i + 1;
}else if(firstTicket == true){
firstTicket = false;
}
}
var runningTotal = parseFloat(ticketCost)*parseFloat(i);
document.getElementById('total').value = runningTotal;
}
Can anyone see what my issue is?
Ta!
i = i +1can be done asi++or asi += 1. If something is true/false, you can doif(firstTicket)and it'll assume it's checking against true orif(!firstTicket)to check against false. Also, if an if/else if only contains a single line, you don't have to use the{}.calculateTotal(this)from the select, the variablethisrefers to the select block. When you saydocument.getElementById(obj).value;(whereobjis thethisyou just passed in) you are trying to check the value of a null object.document.getElementById()accepts an ID (as a string) as an argument, not a DOM object.