I need to create an "if statement" for the below result of a javascript calculation, however the "var ansD" variable won`t let me, any help would be appreciated,
It is the below line in question, i have seperated it out in the script for easier viewing,
Code:
--------------------------------------------------
var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))
----------------------------------------------------
<script type="text/javascript">
var btn = document.getElementById('calculate');
btn.onclick = function()
{
// get the input values
var val1 = parseInt(document.getElementById('val1').value);
var val2 = parseInt(document.getElementById('val2').value);
var val3 = parseInt(document.getElementById('val3').value);
var val4 = parseInt(document.getElementById('val4').value);
var valq1 = parseInt(document.getElementById('valq1').value);
var valq1a = parseInt(document.getElementById('valq1a').value);
var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);
var val4a = parseInt(document.getElementById('val4a').value);
var ansD = document.getElementById("answer");
ansD.value = (val4 + val4a) * (valq1);
var ansD = document.getElementById("answer2");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/5000/100 + val1a * val2a * val3a * 100/5000/100);
var ansD = document.getElementById("answer3");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000/100 + val1a * val2a * val3a*100/1000000/100);
var ansD = document.getElementById("answer4");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000*35.31/100 + val1a * val2a * val3a*100/1000000*35.31/100);
var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))
var ansD = document.getElementById("result_pricea");
ansD.value = '' + Math.round ((val1a * val2a * val3a*100/5000/100 + 15.00 *1.50 +(val4a * 0.68))*(valq1a))
var result_price = parseInt(document.getElementById("result_price").value);
var result_pricea = parseInt(document.getElementById("result_pricea").value);
var ansD = document.getElementById("answer5");
ansD.value =result_price + result_pricea;
// get the elements to hold the results
var result1a = document.getElementById('result1a')
var result1 = document.getElementById('result1')
var result2 = document.getElementById('result2');
var result3 = document.getElementById('result3');
var tot_result3 = document.getElementById('tot_result3');
var result1a = document.getElementById('result1a')
var result2a = document.getElementById('result2a');
var result3a = document.getElementById('result3a');
var tot_result3 = document.getElementById('tot_result3');
var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);
var val4a = parseInt(document.getElementById('val4a').value);
// get the elements to hold the results
// create an empty array to hold error messages
var msg = [];
// check each input value, and add an error message
// to the array if it's not a number
if (isNaN(val1)) {
msg.push('Height is not a number');
}
if (isNaN(val2)) {
msg.push('Width is not a number');
}
if (isNaN(val3)) {
msg.push('Length is not a number');
} if (isNaN(val1a)) {
msg.push('Height is not a number');
}
if (isNaN(val2a)) {
msg.push('Width is not a number');
}
if (isNaN(val3a)) {
msg.push('Length is not a number');
}
// if the array contains any values, display the error message(s)
// as a comma-separated string in the first <span> element
if (msg.length > 0) {
result1.innerHTML = msg.join(', ');
} else if (msg.length > 0) {
result1a.innerHTML = msg.join(', ');
} else {
// otherwise display the results in the <span> elements
result1.innerHTML = '' + Math.round((val1 * val2 * val3*100)/5000)/100;
result1a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/5000)/100;
result5.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000)/100;
result5a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000)/100;
result6.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000*35.31)/100;
result6a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000*35.31)/100;
//START TEST LINE
//END TEST LINE
// calculations for first line on form
{
var x="";
if (+ val1 * val2 * val3/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweight").innerHTML=x;
}
result2.innerHTML = '' + (2*val1 + 2*val2 + val3);
{
var x="";
if (2*val1 + 2*val2 + val3 <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girth").innerHTML=x;
}
result3.innerHTML = '' + (val4);
{
var x="";
if (val4 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weight").innerHTML=x;
}
if (+ val1 * val2 * val3/ 5000 >69)
{
alert("Line 1 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4 >69)
{
alert("Line 1 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1 + 2*val2 + val3 >419)
{
alert("Line 1 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3>270)
{
alert("Line 1 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//1st line of form end
//calculations for 2nd line of form
{
var x="";
if (+ val1a * val2a * val3a/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweighta").innerHTML=x;
}
result2a.innerHTML = '' + (2*val1a + 2*val2a + val3a);
{
var x="";
if (2*val1a + 2*val2a + val3a <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girtha").innerHTML=x;
}
result3a.innerHTML = '' + (val4a);
{
var x="";
if (val4a<70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weighta").innerHTML=x;
}
if (+ val1a * val2a * val3a/ 5000 >69)
{
alert("Line 2 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4a >69)
{
alert("Line 2 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1a + 2*val2a + val3a >419)
{
alert("Line 2 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3a>270)
{
alert("Line 2 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//2nd line of form end
}
};
</script>Any help would be much appreciated,
Regards,
LightBulb Last edited by jscheuer1; Today at 04:40 PM. Reason: Format Edit Post Reply Reply With Quote Thanks .
--------------------------------------------------------------------------------
Today, 04:49 PM #2 jscheuer1
View Profile View Forum Posts View Blog Entries Add as Contact
Global Moderator
Join Date:Mar 2005
Location:SE PA USA
Posts:26,007
Thanks:37
Thanked 2,635 Times in 2,609 PostsBlog Entries:10
It would be a good idea to use a separate variable name for each element that you want to affect. This of course would mean that you would have to change its name in any subsequent code that references it. Unless document.getElementById("result_price"); is a form element, changing its value will do nothing. If it's a span, div, etc., use:
Code:
var ansD = document.getElementById("result_price");
ansD.innerHTML = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))If document.getElementById("result_price"); doesn't exist or hasn't been parsed yet by the browser. The code will create an error and/or do nothing.
Diagnosing this without seeing the page is difficult.
If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out. - John
________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate Reply Reply With Quote Thanks .
--------------------------------------------------------------------------------
Today, 05:09 PM #3 LightBulb
View Profile View Forum Posts View Blog Entries
New Comer (less than 5 posts)
Join Date:Aug 2012
Posts:3
Thanks:0
Thanked 0 Times in 0 Posts
Hi Thank you for your reply,
This is the link [
norspeed.com...] The elemement already exists, this element allows me to calculate the results from the form and to send via a php file to an e-mail address,
I can set up an "if statement" for it if i configuire it for a <span>tag, but as it needs to calculate and then send the calculation to the email i have to configuire it for an<input> tag which is where the problem lies,
I need to code an "if statement" to stop the price being shown in the price box if the size or weights exceed the shipping parameters,
Of course if it is possible to get the <span> tag as opposed to the <input>tag to send the result to the e-mail it would be even better as i can do an "if statement" for that without a problem,
---------------------------------------------------------
I need to create an "if statement" for the below result of a javascript calculation, however the "var ansD" variable won`t let me, any help would be appreciated,
It is the below line in question, i have seperated it out in the script for easier viewing,
Code:
var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))Code:
<script type="text/javascript">
var btn = document.getElementById('calculate');
btn.onclick = function()
{
// get the input values
var val1 = parseInt(document.getElementById('val1').value);
var val2 = parseInt(document.getElementById('val2').value);
var val3 = parseInt(document.getElementById('val3').value);
var val4 = parseInt(document.getElementById('val4').value);
var valq1 = parseInt(document.getElementById('valq1').value);
var valq1a = parseInt(document.getElementById('valq1a').value);
var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);
var val4a = parseInt(document.getElementById('val4a').value);
var ansD = document.getElementById("answer");
ansD.value = (val4 + val4a) * (valq1);
var ansD = document.getElementById("answer2");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/5000/100 + val1a * val2a * val3a * 100/5000/100);
var ansD = document.getElementById("answer3");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000/100 + val1a * val2a * val3a*100/1000000/100);
var ansD = document.getElementById("answer4");
ansD.value = '' + Math.round((valq1) * val1 * val2 * val3*100/1000000*35.31/100 + val1a * val2a * val3a*100/1000000*35.31/100);
var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))
var ansD = document.getElementById("result_pricea");
ansD.value = '' + Math.round ((val1a * val2a * val3a*100/5000/100 + 15.00 *1.50 +(val4a * 0.68))*(valq1a))
var result_price = parseInt(document.getElementById("result_price").value);
var result_pricea = parseInt(document.getElementById("result_pricea").value);
var ansD = document.getElementById("answer5");
ansD.value =result_price + result_pricea;
// get the elements to hold the results
var result1a = document.getElementById('result1a')
var result1 = document.getElementById('result1')
var result2 = document.getElementById('result2');
var result3 = document.getElementById('result3');
var tot_result3 = document.getElementById('tot_result3');
var result1a = document.getElementById('result1a')
var result2a = document.getElementById('result2a');
var result3a = document.getElementById('result3a');
var tot_result3 = document.getElementById('tot_result3');
var val1a = parseInt(document.getElementById('val1a').value);
var val2a = parseInt(document.getElementById('val2a').value);
var val3a = parseInt(document.getElementById('val3a').value);
var val4a = parseInt(document.getElementById('val4a').value);
// get the elements to hold the results
// create an empty array to hold error messages
var msg = [];
// check each input value, and add an error message
// to the array if it's not a number
if (isNaN(val1)) {
msg.push('Height is not a number');
}
if (isNaN(val2)) {
msg.push('Width is not a number');
}
if (isNaN(val3)) {
msg.push('Length is not a number');
} if (isNaN(val1a)) {
msg.push('Height is not a number');
}
if (isNaN(val2a)) {
msg.push('Width is not a number');
}
if (isNaN(val3a)) {
msg.push('Length is not a number');
}
// if the array contains any values, display the error message(s)
// as a comma-separated string in the first <span> element
if (msg.length > 0) {
result1.innerHTML = msg.join(', ');
} else if (msg.length > 0) {
result1a.innerHTML = msg.join(', ');
} else {
// otherwise display the results in the <span> elements
result1.innerHTML = '' + Math.round((val1 * val2 * val3*100)/5000)/100;
result1a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/5000)/100;
result5.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000)/100;
result5a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000)/100;
result6.innerHTML = '' + Math.round((val1 * val2 * val3*100)/1000000*35.31)/100;
result6a.innerHTML = '' + Math.round((val1a * val2a * val3a*100)/1000000*35.31)/100;
//START TEST LINE
//END TEST LINE
// calculations for first line on form
{
var x="";
if (+ val1 * val2 * val3/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweight").innerHTML=x;
}
result2.innerHTML = '' + (2*val1 + 2*val2 + val3);
{
var x="";
if (2*val1 + 2*val2 + val3 <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girth").innerHTML=x;
}
result3.innerHTML = '' + (val4);
{
var x="";
if (val4 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weight").innerHTML=x;
}
if (+ val1 * val2 * val3/ 5000 >69)
{
alert("Line 1 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4 >69)
{
alert("Line 1 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1 + 2*val2 + val3 >419)
{
alert("Line 1 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3>270)
{
alert("Line 1 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//1st line of form end
//calculations for 2nd line of form
{
var x="";
if (+ val1a * val2a * val3a/ 5000 <70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_volweighta").innerHTML=x;
}
result2a.innerHTML = '' + (2*val1a + 2*val2a + val3a);
{
var x="";
if (2*val1a + 2*val2a + val3a <419)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_girtha").innerHTML=x;
}
result3a.innerHTML = '' + (val4a);
{
var x="";
if (val4a<70)
{
x="OK";
}
else
{
x="X";
}
document.getElementById("result_actual_weighta").innerHTML=x;
}
if (+ val1a * val2a * val3a/ 5000 >69)
{
alert("Line 2 - The Volumetric Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
if (val4a >69)
{
alert("Line 2 - The Actual Weight Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (2*val1a + 2*val2a + val3a >419)
{
alert("Line 2 - The Girth + Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
else if (val3a>270)
{
alert("Line 2 - Length Exceeds The Limits For Standard Parcel Delivery - Please Contact Us For Alternative Delivery Options");
return false;
}
//2nd line of form end
}
};
</script>