Thursday, 15 January 2015

html - Javascript not working with adding variables together -



html - Javascript not working with adding variables together -

right have values in 3 dropdown boxes , there more (looking @ adding 20 dropdown boxes add together give final cost item. here code 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 first box going 20 items--> </select> <p>testing 2</p> <select id="test2"> <option value="25">item one</option> <option value="100">item two<option> <!--i have sec box going 10 items--> </select> <p>testing 3</p> <select id="test3"> <option value="24">item one</option> <option value="100">item two<option> <!--i have 3rd box going 10 items--> </select> </form> <button onclick="myfunction()">update</button> <p id="demo"></p>

then have javascript

function myfunction() { var = document.getelementbyid("list1"); var = 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 works if take var c our , take var y out , have calculate x. have tryed many ways , @ loss. cheers

p.s. java script in script tag @ bottom

you've used a.selectedindex selected item 3 elements. alter utilize b.selectedindex , c.selectedindex in appropriate spots. or can a.value, b.value , c.value straight rather going via options collection.

also id attributes in html don't match you're using in js.

fix problems , works, can see here: http://jsfiddle.net/6wwdc/

p.s. it's thought in habit of passing radix sec parameter parseint(), inta = parseint(a, 10); - otherwise if input string has leading 0 or leading 0x might (depending on browser, , presense of "use strict"; directive) interpreted octal or hexadecimal.

javascript html

No comments:

Post a Comment