aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Adding prices using Javascript    Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Adding prices using Javascript    " Watch "Adding prices using Javascript    " New topic
Author

Adding prices using Javascript

Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
I am relativley new to javascript, I have previously used it only to make simple java applets; but recently I have been trying to lear more about the language.

I have decided to make an online store for my small computer buisness, but I came across a hurdle when designing the computer customisation section. I want customers to be able to see the 'live' total cost of there computer system as they add components to it (using a form). At the moment the form sends the selected data back to me by email and I have to add the prices up and send a reply email to the customer.

This is somewhat impractical and I thought that javascript would be the right solution to add the selected component prices on the webpage.

Can anyone help with the scripting?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by JJ-Jimi:
I am relativley new to javascript, I have previously used it only to make simple java applets; but recently I have been trying to lear more about the language.

Applets are written in Java, not JavaScript, so it's not clear which language you are really asking about.

Java and JavaScript have little in common except for the unfortunate naming. (Netscape thought that JavaScript sounded better than ECMAScript back in the 90's).

So which is it? Are you asking about server-side Java? Or client-side JavaScript?

I suspect the latter, but want to be sure.
[ January 26, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
Sorry, I am talking about javascript client-side.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Cool. I'll move this off to our HTML/JavaScript forum. See you there.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Decide what event you want to handle in order to trigger the addition. Do you want it to happen whenever they leave a field, or when they click a "calculate" button?

Establish an event handler (or handlers) for that event, grab the field values, do the math, and display the result.

While this is not rocket science, it touches on a lot of JavaScript areas. Do you have a good reference?
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
Yh i want it to happen whenever they leave a field. I have got a book 'Java Made Simple' (second edition) I'm not sure that it includes all the scipting i need.
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
My main issue is getting it to do the calculation when the user leaves the field. And also converting the inputed data (from option buttons) into integer format so it can be used in calculations.

At the moment I can convert the data uing 'parseint' but the new integer variable that is created doesnt update when another option is selcted by the user.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jimi Warren:
Yh i want it to happen whenever they leave a field. I have got a book 'Java Made Simple' (second edition) I'm not sure that it includes all the scipting i need.


As Bear mentioned earlier, Java and JavaScript are two totally different technologies who's names both happen to start with the same four letters.
A book on Java isn't going to be of much use to you if you are programming on the client with JavaScript.
[ January 26, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Originally posted by Jimi Warren:
but the new integer variable that is created doesnt update when another option is selcted by the user.

Sounds like you've got a bit of code working. How about posting the relevant portions? Please be sure to use UBB code tags when posting code. Read this for more information.
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
I was a bit confused between the difference between java and javascript so the code I have got is pretty much useless as I have written it as a java applet which isnt really suitable. I have ordered a book from amazon about javascipr opposed to java so I can learn a bit more.
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
I will attempt to write the basic code I have in javscript using internet script sources. Then I will post that.
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
Right. I have now got a book on javascript 'Head First JavaScript'. I have decided to write the script from scratch as I basically don't have any working script. I think I understand how to get the field values, and display the result. But i don't understand what event handler(s) i need and how to convert the field value into an integer.

Is anyone able to help on that?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

The onblur event will fire whenever a user leaves a field. onchange will fire if the field has been changed. You can pick the one that best suits your situation.

To convert a text value to its numeric equivalent, check out the window.parseInt() method.

Be aware that event handling is one of the more difficult areas to handle correctly in a cross-browser fashion. That is why many page authors will employ a library (such as Prototype or jQuery) to abstract these difference for them.
Jimi Warren
Greenhorn

Joined: Jan 26, 2008
Posts: 10
Ok, cheers.

Will it be best to use 'if statements' to display [+�--] next to the item 'if' the item is not selected?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding prices using Javascript