Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-25-2012, 09:05 PM   PM User | #1
Razzy
New Coder

 
Join Date: Jun 2012
Posts: 41
Thanks: 14
Thanked 1 Time in 1 Post
Razzy is an unknown quantity at this point
Calculating values from multiple dropdown menus, total value outputs on textbox

hello people,

i am creating a website for a meatshop and i am stuck on the order form page which requires a fair bit of javascript which i am quite new to, on the order form i have 4 dropdown menus, with all 4 having similar values:

Code:
<script type="text/javascript">
function addDrops() {
var sausageOne = document.getElementById( 'sausage1');
var sausageTwo = document.getElementById( 'sausage2');
document.getElementById( 'output').value = parseFloat(sausage1.options[ sausage1.options.selectedIndex ].value
) + parseFloat(sausage2.options[ sausage2.options.selectedIndex ].value
);
)
</script>



<body>


<SELECT name="sausage1" id="sausage1" onchange="addDrops()" >
<OPTION VALUE="" >
<OPTION value="5.99">KQF bolly Chicken Sausages      
<OPTION value="4.00">KQF bolly Lamb Sausages          
<OPTION value="3.99">KQF Jumbo Lamb Sausages          
</SELECT>
		  
<SELECT name="sausage2" id="sausage2" onchange="addDrops()">
<OPTION VALUE="" >
<OPTION value="5.99">KQF bolly Chicken Sausages      
<OPTION value="4.00">KQF bolly Lamb Sausages          
<OPTION value="5.99">KQF Jumbo Lamb Sausages          
</SELECT>
		  
<SELECT name="sausage2" id="sausage3" onchange="addDrops()">
<OPTION VALUE="" >
<OPTION value="5.99">KQF bolly Chicken Sausages      
<OPTION value="4.00">KQF bolly Lamb Sausages          
<OPTION value="5.99">KQF Jumbo Lamb Sausages          
</SELECT>
		  
<SELECT name="sausage2" id="sausage4" onchange="addDrops()">
<OPTION VALUE="" >
<OPTION value="5.99">KQF bolly Chicken Sausages      
<OPTION value="4.00">KQF bolly Lamb Sausages          
<OPTION value="5.99">KQF Jumbo Lamb Sausages          
</SELECT>

<input type="text" id='output'/>


</body>
when i select an option from the first dropdown menu the output should automatically appear on the textbox, and if i select an option from another dropdown menu then the value of the other dropdown menu should add to the value of the first dropdown menu etc. ive been trying to use the javascript above but it does not work

would really appreciate it if someone could help

thank you
Razzy is offline   Reply With Quote
Old 06-25-2012, 10:17 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The form makes no sense.

Why would you need 4 <select>s when you only have 3 items in each one? At most, you would need 3 <select>s.

On top of that, what do you do if somebody wants to order SEVEN (7) Chicken Sausages??? You need QUANTITY <select>s or <input>s in addition to the meat selections.

Or is this homework for some class? It feels like that to me, because homework doesn't have to be realistic or work in the real world.

********

By the way, you do *NOT* have to do
Code:
    sausage1.options[ sausage1.options.selectedIndex ].value
You only had to do that back in the days of Netscape version 4 (1999 or so). All modern browser allow you do just do
Code:
    sausage1.value
to get the same thing.

Anyway, you have several mistakes in your JavaScript code.
Code:
function addDrops() {
    var sausageOne = document.getElementById( 'sausage1');
    var sausageTwo = document.getElementById( 'sausage2');
    document.getElementById( 'output').value = 
            parseFloat(sausage1.options[ sausage1.options.selectedIndex ].value) 
        + parseFloat(sausage2.options[ sausage2.options.selectedIndex ].value
    );
) // you are using right paren instead of right brace!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Razzy (06-25-2012)
Old 06-25-2012, 10:28 PM   PM User | #3
Razzy
New Coder

 
Join Date: Jun 2012
Posts: 41
Thanks: 14
Thanked 1 Time in 1 Post
Razzy is an unknown quantity at this point
sorry there is about 8 values within the dropdown menus, i only added three so far, i wanted to make sure it worked first before completing the dropdown menu, sorry my bad
thanx for picking out the mistakes, i am new to javascript
Razzy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:22 AM.


Advertisement
Log in to turn off these ads.