homepage Welcome to WebmasterWorld Guest from 23.20.91.134
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
if statement
if statement
LightBulb




msg:4489125
 3:11 pm on Aug 28, 2012 (gmt 0)

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,

var ansD = document.getElementById("result_price");
ansD.value = '' + Math.round ((val1 * val2 * val3*100/5000/100 + 15.00 * 1.50 +(val4 * 0.68))*(valq1))

Regards,
LightBulb

 

lucy24




msg:4489184
 5:18 pm on Aug 28, 2012 (gmt 0)

What exactly do you need your IF statement to say? Come clean now; we've all been there. In particular, is this supposed to be a string or a number? It seems as if you've left out a step.

Did you remember to double the == sign? I'm not trying to be insulting; I do it myself about twice a week on average. ("Helloo? Function? We both know you're not supposed to return 'false' on all 256 iterations.")

LightBulb




msg:4489187
 5:26 pm on Aug 28, 2012 (gmt 0)

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>

lucy24




msg:4489680
 6:21 am on Aug 30, 2012 (gmt 0)

Oh, my head hurts. What did you do there-- cut & paste an entire thread from some other forum? TMI, I think. It's the exact same code twice, with intervening dialogue.

the "var ansD" variable won't let me

What does this mean? How do you know ansD is the culprit? One of the special features of javascript-- well, of almost any programming language-- is that it won't tell you what you did to make it mad. It just sits there and plays dead until you figure it out.

Here, as far as I can tell, "ansD" isn't anything. It's just a temporary holder for a series of actions: find input area, fill with specified text.

Why is it re-declared every time? (Detour here to look it up. Turns out it isn't technically an error-- but it still strikes me as a Very Bad Idea, because it encourages you to forget that you've already used this variable in this function.)

Text editor obligingly finds all seven sets for me:

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 ansD = document.getElementById("answer5");
ansD.value =result_price + result_pricea;


Is that supposed to be five strings and two numbers? Is something missing in the last line?

Don't know about anyone else, but sequences like
(valq1) * val1 * val2 * val3*100/1000000*35.31/100
make me nervous. I'd be slathering on the parentheses at this point ;) And-- this is Voice Of Experience speaking-- it never hurts to specify the base in parseInt. Sometimes it's a lifesaver.

birdbrain




msg:4489713
 8:57 am on Aug 30, 2012 (gmt 0)

Hi there lucy24,

put this one to bed. :)

"LightBulb's" problem appears to be answered by the more than capable "John Scheuer" over at Dynamicdrive Forums. ;)



birdbrain

lucy24




msg:4489921
 5:44 pm on Aug 30, 2012 (gmt 0)

Double ouch :) Thanks to the same trusty text editor, I see there were NINE items with the same id ("result_pricea", not to be confused with "result_price"). I also see liberal use of &nbsp; for formatting, but happily this is not the HTML or CSS forum.

... and that's why the "Why We Make You Do It Yourself" apache boilerplate includes the paragraph
... when someone with a low post count asks a question, that person may have posted the identical question in six different forums. They grab the first answer they get, and never even come back to the others. You can understand that this is infuriating for the person who took the time to compose an answer.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved