Jump to content

Simple Javascript if else


Recommended Posts

Here's another one I'm having trouble making work.

I want to ask for a quantity and then fill in the fee based on the quantity answer.

If the quantity is 0 the fee will be 0 if the quantity is 4 the fee will be $40.

The error is "document.getElementId("EditRecordDirectory_Fee") is null

function calculate()

{

var quantity = parseFloat(document.getElementById("EditRecordDirectory_Quan").value);

var price = parseFloat(10);

var total = parseFloat(quantity * price);

if (quantity == 0)

{document.getElementById("EditRecordDirectory_Fee").value = 0;}

else

{document.getElementById("EditRecordDirectory_Fee").value = Math.round(total);}

}

document.getElementById("caspioform").onsubmit=calculate;

Link to comment
Share on other sites

I see that you have provided only one condition in if statement. i.e if (quantity == 0). What happens when quantity is Null?? Therefore, you have to check in a such way that the condition is satisfied if quantity is a number.

For example:

if(!isNaN(quantity))

{document.getElementById("EditRecordDirectory_Fee").value = 0;}

else

{document.getElementById("EditRecordDirectory_Fee").value = Math.round(total);}

hope it helps... :)

Link to comment
Share on other sites

Okay, I made the following modification (see below), with the same error (document.getElementBy....is null). The script is in the footer of the datapage.

function calculate_directory()

{

var quantity = parseFloat(document.getElementById("EditRecordDirectory_Quan").value);

var price = parseFloat(10);

var total = parseFloat(quantity * price);

if(!isNaN(quantity))

{document.getElementById("EditRecordDirectory_Fee").value = 0;}

else

{document.getElementById("EditRecordDirectory_Fee").value = Math.round(total);}

}

document.getElementById("caspioform").onsubmit=calculate_directory;

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...