Monday, 15 February 2010

html - Not adding 2 numbers correctly in javascript -



html - Not adding 2 numbers correctly in javascript -

i have drop downwards list in html values show here,

<select id="thedropdown"> <option value="1">test1</option> <option value="2" selected="selected">test2</option> <option value="3">test3</option> </select>

and javascript:

function myfunction() { var = document.getelementbyid("thedropdown"); var b = a.options[a.selectedindex].value; var y=b; var x=y+2; var demop=document.getelementbyid("demo") demop.innerhtml= x; }

however reply when click button makes 22 when should 4. can see problem. cheers help in advance

right code concatenates strings. need parseint() or parsefloat() (or number() or +) values before adding.

when using parseint() create sure always specify sec argument (the radix):

b = parseint(a.options[a.selectedindex].value, 10);

javascript html function variables drop-down-menu

No comments:

Post a Comment