0

Right so i have values in 3 dropdown boxes and there will be more (looking at adding about 20 dropdown boxes that add to give a final price for the item. Here is the code for the HTML.

<form id="myform1>
<p>Testing 1</p>
<select id="Test1">
<option value="24">Item one</option>
<option value="29">Item Two<option>
<!--I have the first box going to 20 items-->
</select>

<p>Testing 2</p>
<select id="Test2">
<option value="25">Item one</option>
<option value="100">Item Two<option>
<!--I have the second box going to 10 items-->
</select>

<p>Testing 3</p>
<select id="Test3">
<option value="24">Item one</option>
<option value="100">Item Two<option>
<!--I have the third box going to 10 items-->
</select>


</form>
<button onclick="myFunction()">Update</button>
<p id="demo"></p>

Then i have my Javascript

function myFunction()
{
var a = document.getElementById("list1");
var A = a.options[a.selectedIndex].value;

var b = document.getElementById("list2");
var B = b.options[a.selectedIndex].value;

var c = document.getElementById("list3");
var C = c.options[a.selectedIndex].value;

var IntA = parseInt(A);
var IntB = parseInt(B);
var IntC = parseInt(C);

var x=IntB + IntA;
var y=x + IntC;
var demoP=document.getElementById("demo")
demoP.innerHTML= y;
}

My Problem is it only works if i take var c our completely and then take var y out and have it calculate to x. I have tryed many ways and am at a loss. Cheers

P.S. Java script is in a script tag at the bottom

2 Answers 2

3

You've used a.selectedIndex to get the selected item for all three elements. Change that to use b.selectedIndex and c.selectedIndex in the appropriate spots. Or you can just say a.value, b.value and c.value directly rather than going via the options collection.

Also the id attributes in your html don't match what you're using in your JS.

Fix those problems and it works, as you can see here: http://jsfiddle.net/6WWdc/

P.S. It's a good idea to get in the habit of always passing the radix as the second parameter to parseInt(), like IntA = parseInt(A, 10); - otherwise if the input string has a leading 0 or leading 0x it might (depending on browser, and presense of a "use strict"; directive) be interpreted as octal or hexadecimal.

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

2 Comments

... Or you can extract common functionality and stop repeating yourself!
cheers i noticed the names after i put it on lol i was just using it as a example will try the code now cheers
0
function myFunction()
{
    var a = document.getElementById("list1");
    var A = a.options[a.selectedIndex].value;

    var b = document.getElementById("list2");
    var B = b.options[b.selectedIndex].value; //changed to var b

    var c = document.getElementById("list3");
    var C = c.options[c.selectedIndex].value; //changed to var c

    //...
}

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.