In the click function for btnGetTotalsSilver
, millesimi is just a number. When the user selects the topmost option in the select, millesimi becomes 0, nothing else. So it is not intrinsically connected with that specific select element that happens to have "millesimi" for an id!
So, when you do document.getElementsByTagName("option")
a while later, you get an array of all the options in the page. This array starts with the very first one (which is in the first select
). So indexing with [millesimi]
(if millesimi is 0) returns the first option in the first select.
Solution: get only the options from the proper select.
Use document.getElementById('millesimi').getElementsByTagName("option")
(or its jQuery equivalent.)
See new fiddle.
Edit: Oh, of course you should do the same for the other select(s) on the page if they have similar handlers.