0

Here is my JSP code:

       <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
           pageEncoding="ISO-8859-1"%>
       <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
       <html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
       <title>Order a PIZZA</title>

       </head>
       <body>
       <b>SUBIR'S JUST IN PIZZA</b><br>
       <i>All PIZZAs in just 30 minutes</i>
       <div align="left">
       Pizza=150/-<br>
       Garlic Bread=60/-<br>
       Soft drink=45/-<br>
       Extra Cheese=45/-<br>
       Tax=12.25%
       </div>
        <div align="right">
            Name:
            <input type="text"><br>
            Number of Pizza:
            <input type="text" id="pizza" onchange="pizza_calculate()"><br>
            Number of Garlic breads:
            <input type="text"><br>
            Number of Soft drinks::
            <input type="text"><br>
            Number of Extra cheese:
            <input type="text"><br>
            Total:
            <input type="text" id="total"><br>
            Tax:
            <input type="text"><br>
            <input type="button" value="reload">
            <input type="submit" value="submit">
            <script type="text/javascript" src="evaluate.js"></script>
        </div>
       </body>
       </html>

My evaluate.JS javascript is the following:

var price=0;
var tax=0;
var pizza=150;
var garlicBread=60;
var softdrink=45;
var extraCheese=45;
var pizza_total=0;
var softdrink_total=0;
function pizza_calculate()
{
var test=document.getElementById("pizza");
console.log(test);
pizza_total=document.getElementById("pizza")*pizza;
document.getElementById("total").innerHTML =pizza_total;
};

From Firebug I understand that my JS function is not getting executed. What might possibly be the reason for this error, please suggest.

8
  • Try using pizza_calculate = function() { ...your function...} Commented Dec 8, 2014 at 21:49
  • 4
    pizza_total=document.getElementById("pizza")*pizza; That won't work. Commented Dec 8, 2014 at 21:50
  • What is *pizza supposed to do? What is pizza? Also, document.getElementById("pizza") returns the element, not its value. Commented Dec 8, 2014 at 21:50
  • 1
    @RocketHazmat, Pizza is a variable here consisting value of each pizza; it is supposed to get multiplied with number of pizzas. Commented Dec 8, 2014 at 21:53
  • 1
    "From Firebug I understand that my JS function is not getting executed." what does that mean? What happens? Do you have error messages? If you do, maybe that would be helpful to include in your question? Commented Dec 8, 2014 at 21:56

3 Answers 3

5

This:

pizza_total=document.getElementById("pizza")*pizza;

should be:

var pizza_total = parseInt( document.getElementById("pizza").value, 10) * pizza;

Or, since you already have the element as a test variable, simply:

var pizza_total = parseInt(test.value, 10) * pizza;

UPDATE

var price = 0;
var tax = 0;
var pizza = 150;
var garlicBread = 60;
var softdrink = 45;
var extraCheese = 45;
var pizza_total = 0;
var softdrink_total = 0;

function pizza_calculate() {
  var test = document.getElementById("pizza");
  var pizza_total = parseInt(test.value, 10) * pizza;
  document.getElementById("total").value = pizza_total;
};
<div align="right">
  Name:
  <input type="text">
  <br>Number of Pizza:
  <input type="text" id="pizza" onchange="pizza_calculate()">
  <br>Number of Garlic breads:
  <input type="text">
  <br>Number of Soft drinks::
  <input type="text">
  <br>Number of Extra cheese:
  <input type="text">
  <br>Total:
  <input type="text" id="total">
  <br>Tax:
  <input type="text">
  <br>
  <input type="button" value="reload">
  <input type="submit" value="submit">
</div>

Note, if you wish instant updates, you can use onkeyup instead of onchange.

Sign up to request clarification or add additional context in comments.

7 Comments

You should fetch values from an input tag with .value, not with .innerHTML.
Just to help the guy who asked the question: parseInt(x,10); makes sure the value you send is an integer compared on base 10. More info on MDN. Good job Shomz!
Yes, thank you for the addition, @JeffNoel! 150 * test.value would probably work, but parseInt is always better.
My intenetion was to autopopulate the total value of pizzas ordered in the total textbox. However still after doing the modifications it is not getting populated. Why is that?Any idea?
See the live example, you had one more error: document.getElementById("total").value = pizza_total; note we're again using value here, not innerHTML.
|
4

You only grab the DOMElement with the ID Pizza, not its value. Try adding .value to get the element's value. Like so:

pizza_total= document.getElementById("pizza").value * pizza;

5 Comments

The pizza is in scope. It's in the vars above the function.
My bad, I'll fix that. That was my first assumption after it returned NaN. Then I realized he wasn't getting the value. Consider it's always fixed!
upvoted for explaining pareseInt(x,10) in the comment of the above answer
Hmm, now I see you answered this correctly two minutes before I did. Not sure why I didn't see it when I was writing an answer. Yours should be accepted (even without the parseInt). At least I gave you an upvote.
@Shomz As long as the question is answered I'm quite happy with the result.
3

Other people have pointed out a number of problems with your script, but the reason your script isn't running is because you have this:

 <input type="text" id="pizza" onchange="pizza_calculate()"><br>

parsed before the script is loaded at the end of your html.

pizza_calculate isn't defined.

Either move your script to top, or better, don't inline your event handlers and instead use addEventListener instead.

function pizza_calculate()
{
    /* body of pizza_calculate */
}
var pizzaInput = document.getElementById("pizza");
pizzaInput.addEventListener("change", pizza_calculate, false);

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.