1

I have a question about the if statement in js. I made a statement, but it doesn't seems to work. I want to check these statements from one single click on "button 1". The script is to check if there are any transportation costs before I calculate the total. Can someone tell me how to fix my problem?

Here's my script:

<SCRIPT TYPE="text/javascript">
function berekeningen()
{

if (document.getElementById("DropdownMenu1").value = "Eeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 20)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Adegem" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Kaprijke" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Lembeke" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Oosteeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Sint-Laureins" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Ursel" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Waarschoot" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Zomergem" && document.getElementById("Order_Subtotal").firstChild.data >= 25)
  {
  document.getElementById("Transportation_Cost").value="0.00";
  }
else
  {
  document.getElementById("Transportation_Cost").value="3.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Knesselare")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Lovendegem")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Bassevelde")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }

if  (document.getElementById("DropdownMenu1").value = "Maldegem")
  {
  document.getElementById("Transportation_Cost").value="5.00";
  }
}
</SCRIPT>

Edit: I changed the script a bit with variables but it doesn't work

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.js"></script>

<SCRIPT TYPE="text/javascript">
function berekeningen()
{
   var plaats           = document.getElementById("plaats").value;
   var kosten           = document.getElementById("kosten").value;
   var subtotaal        = document.getElementById("Bestelling_subtotaal").firstChild.data
   var class_1_plaatsen = new Array("Eeklo");
   var class_2_plaatsen = new Array("Adegem", "Kaprijke", "Lembeke", "Oosteeklo",
                                    "Sint-Laureins", "Ursel", "Waarschoot", "Zomergem");
   var class_3_plaatsen = new Array("Knesselare", "Lovendegem", "Bassevelde", "Maldegem");

   if ($.inArray(plaats, class_1_plaatsen) >= 0) {

      // Berekening voor plaatsen in prijscategorie 1 hieronder.

   }
   else if ($.inArray(plaats, class_2_plaatsen) >= 0) {

      // Berekening voor plaatsen in prijscategorie 2 hieronder.

   }
   else {

      // Berekening voor overige plaatsen hieronder.

   }
}
</SCRIPT>

I wan't to make this:

When var plaats = class_1_plaatsen and subtotaal < 15, then alert (We leveren vanaf minimum € 15). But if subtotaal is between 15 and 20, then kosten = 3. But if subtotaal >= 20, then kosten = 0.

When var plaats = class_2_plaatsen and subtotaal < 20, then alert (We leveren vanaf minimum € 20). But if subtotaal is between 20 and 25, then kosten = 3. But if subtotaal >= 25, then kosten = 0.

When var plaats = class_3_plaatsen and subtotaal < 20, then alert (We leveren vanaf minimum € 20). But if subtotaal >= 20, then kosten = 5.

Dyasten

1
  • jslint.com, FIY: = is an assignment operator, == comparison, === strict comparison (that's what you should use). Commented Aug 13, 2012 at 23:00

2 Answers 2

1

Shouldn't all your if statements have two ==

if  (document.getElementById("DropdownMenu1").value == "Knesselare")
Sign up to request clarification or add additional context in comments.

Comments

1

Generally you don't want to use the single = sign in an if statement; it evaluates as a new variable assignment.

Instead of = use == like this:

if (document.getElementById("DropdownMenu1").value == "Eeklo" && document.getElementById("Order_Subtotal").firstChild.data >= 20)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.