![]() |
Submit button not working
When I click calculate now, a prompt is supposed to come up but nothing happens and I can't figure out where the error in my code is. Any suggestions is appreciated.
Code:
<html> |
You're trying to add objects, not the value of the objects.
var adult = document.getElementById('adult').value; Also, + in JS means "concatenate" unless you indicate that it needs to be math. parseFloat(adult + child + senior... ); |
I added the .value and the parseFloat, but I still don't know why the prompt from displayTotal() wont appear.
UPDATED CODE Code:
<html> |
Try alert instead of prompt.
|
alert did not work
|
Place the <script> tag and contents just before the closing </body> tag. Also, check your error console to see if there are any messages.
|
1 Attachment(s)
I moved the <script> and its contents and I went into error console in chrome but it's blank. I wasn't sure if I looked in the right place for the console so I took a screenshot in the image below of where I was.
|
Quote:
You persistently use Id instead of id - Javascript is case sensitive. Id is meaningless to Javascript. You also persistently use document.getElementbyId - again, Javascript is case sensitive. onClick should be onclick. You also use the same id for HTML elements and Javascript variables. Very unwise. Use Number() to convert the string values entered by the user:- var Adult = Number(document.getElementbyId('adult').value); // Javascript is case sensitive so Adult is not the same as adult // etc. var total = parseFloat(adult + child + senior + military + seniorday + studentday); // concatenates the 6 string values and converts to a real number. Use Number() to make the individual values numbers, and remove parseFloat(). In any case the number of tickets must be an integer, so parseFloat() makes no sense. You have not specified the width and height of your image. You have assigned the same name quantity to multiple variables. May be other errors as well. My advice would be to strip your code down to just one element to start with, test that and get it working before moving on. |
Code:
function displayTotal(){ |
Quote:
var Adult = Number(document.getElementById('adult').value) || 0; // and so on parseFloat() is not required. Code:
function displayTotal(){ |
Ok I changed a bunch of stuff and I got the prompt working as well but when I add a 2 in a textbox and a 3 in another. It outputs 23 not 5??? Also how would I hardcode each ticket with a certain price like for example adult price is $10.50. So when I enter a 4 for quantity it multiplies 10.50 with 4 and outputs its?
Code:
<html> |
You changed all of the getElements to name'Ticket' without changing the corresponding input names/ids.
|
If the error console isn't giving you enough information about what is wrong and you can't figure out how to use the debugger (or are using Firefox and haven't installed one) then use alert() confirm() or prompt() to display values at various points so you can follow the code execution - that's what those dialogs are for.
|
You code is still rotten with errors.
One more time: You changed all of the getElements to name'Ticket' without changing the corresponding input names/ids. input Id="adult" If you used Number() as I advised then 2+3 comes to 5, not 23 which is two string values concatenated. I don't think that the code you posted here is the same code as you are using to test. :( To multiply the number of tickets by price simply do var Adult = Number(document.getElementById('adultTicket').value) || 0; var aduitprice = 10.50; // (no $ sign!!) var adultcost = Adult * adultprice; If desired add a $ sign to the total at the end before displaying the result. |
Quote:
Code:
<html> |
| All times are GMT +1. The time now is 07:00 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.