You are viewing our Forum Archives. To view or take place in current topics click here.
Javascript help
Posted:

Javascript helpPosted:

FR0NTLinealpaca
  • Ladder Climber
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Status: Offline
Joined: Feb 26, 201212Year Member
Posts: 365
Reputation Power: 15
Making a very simple ordering in which the user types the number of how many of each items they want into different textareas but for some reason I cannot get the button that calls the function to finalise the order to work. any help you'll be apprectiated.
<input type="button" value="Click to order" id="orderButton" onclick="Order()" />

<script>
   
      <!-- extra functions to check if input is an integer -->
      function isInt(n) {
        return n % 1 === 0;
      }

      function Order() {
        var amountB = document.getElementById("blueOrder").value;
        var amountR = document.getElementById("redOrder").value;
        var amountY = document.getElementById("yellowOrder").value;
        var amountG = document.getElementbyId("greenOrder").value;
        var text = document.getElementbyId("desiredText").value;
        var total = amountB + amountR + amountY + amountG;
       
        if (isInt(document.document.getElementById("blueOrder").value) == true && isInt(document.getElementById("redOrder").value) == true && isInt(document.getElementById("yellowOrder").value) == true && isInt(document.getElementbyId("greenOrder").value) == true) {
          if (total > 100) {
            var cost = (total * 0.03);
            if (confirm("CONFIRM ORDER: /n blue bags: " + amountB + "/n red bags: " + amountR + "/n yellow bags: " + amountY + "/n green bags: " + amountG + "/n Desired Text: " + text)) {
              alert("Order Confirmed");
            } else {
              alert("Order Cancelled");
            }
          } else {
            alert("Minimum order is 100 bags.");
          }
        }else {
          alert("One or more of the forms doesn't contain a quantity for order.");
        }
#2. Posted:
ecco2k
  • Hide and Seek
Status: Offline
Joined: Oct 22, 201013Year Member
Posts: 830
Reputation Power: 458
Status: Offline
Joined: Oct 22, 201013Year Member
Posts: 830
Reputation Power: 458
I would of gone about getting this function to work differently but that's just me, you can PM if you want some help sorting it.

Do you get any errors in the console when this script is ran?
#3. Posted:
ZZ9_x_iCaLZz
  • Challenger
Status: Offline
Joined: Jun 23, 201211Year Member
Posts: 113
Reputation Power: 7
Status: Offline
Joined: Jun 23, 201211Year Member
Posts: 113
Reputation Power: 7
Errors:
   
Line 3:
   [code]<!-- -->[/code] These tags are only valid inside html not Javascript
   use
   // for a line or console.log("IM COMMENTED OUT");
   /* For a enclosed multiline or single line*/ console.log("I WORK");

Line 12-13:
   document.getElementbyId <-- this is spelled incorrectly (getElement B yId)

NOTE:
   Its \n for new line escape character, not /n
   
Fixed code:

JAVASCRIPT:
// http://www.thetechgame.com/Forums/t=7525476/javascript-help.html
// <!-- extra functions to check if input is an integer -->

function isInt(n) {
   return n % 1 === 0;
}


function Order() {
   // Get Input
   var amountB = document.getElementById("blueOrder")      .value;
   var amountR = document.getElementById("redOrder")      .value;
   var amountY = document.getElementById("yellowOrder")   .value;
   var amountG = document.getElementById("greenOrder")      .value;
   var text    = document.getElementById("desiredText")   .value;
   
   // Log Input into console (REMOVE IT WHEN FINISHED TESTING)
   console.log(amountB, amountR, amountY, amountG, text);
   
   // Calculate Total
   var total    = amountB + amountR + amountY + amountG;
   
   // Create our flags
   var intB    = isInt(amountB);
   var intR    = isInt(amountR);
   var intY    = isInt(amountY);
   var intG    = isInt(amountG);
   var flags    = (intB && intR && intY && intG);
   
   // Log our flags
   console.log(intB, intR, intY, intG, flags);
   
   if (flags) {
      if (total > 100) {
         var cost = (total * 0.03);
         
         var outputToBeDisplayed =
            "CONFIRM ORDER: "    + "\n" +
            "--------------"    + "\n" +
            "Blue bags: "       + amountB + "\n" +
            "Red bags: "       + amountR + "\n" +
            "Yellow bags: "    + amountY + "\n" +
            "Green bags: "       + amountG + "\n" +
            "Desired Text: "    + text;
         
         if (confirm(outputToBeDisplayed)) {
            alert("Order Confirmed");
         } else {
            alert("Order Cancelled");
         }
      } else {
         alert("Minimum order is 100 bags.");
      }
   } else {
      alert("One or more of the forms doesn't contain a quantity for order.");
   }
}


CODEPEN: [ Register or Signin to view external links. ]
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.