/    Sign up×
Community /Pin to ProfileBookmark

value * 2 from <select> becomes wrong ?

Hello, well i’m kind of new to javascript and keep getting wrong sum.

starts like this

<select id=”numbers” onChange=”change()”>
<option value=”1″>1
<option value=”2″>2
<option value=”3″>3
<option value=”4″>4
</select>

so far so good, from here i want the number on the page to change depending on what value is chosen like this

var age=document.getElementById(‘myTable’).rows[1].cells;

age[0].innerHTML=personListt.options[personListt.selectedIndex * 2].value;

now when i try it while 1 is being selected the number is just 1 instead of 2,
further on and when i choose 2 i get the number 3 instead of 4, and from value 3 & 4 the number just stays at 3.

any idea on what to do ?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 13.2008 — since the index's start @ 0, not 1 that's perfectly normal.

0 -- 0(first option)
1 -- 2(3rd option)
2 -- 4(no option erk.)
3 -- 6(as above)

you could always try:
[code=php]age[0].innerHTML=personListt.options[(personListt.selectedIndex+1) * 2].value;[/code]but that still doesn't fix the problem with numbers far above what you have in your list...
Copy linkTweet thisAlerts:
@cepeslanauthorFeb 13.2008 — got the returned value to be as it should until i choose 3 or 4.

[CODE]
<select id="numbers" onChange="change()">
<option value="0">0
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
</select>
[/CODE]


worked sweet when i added the 0 option and the parantheses

[CODE]age[0].innerHTML=numbers.options[(numbers.selectedIndex) * 2].value; [/CODE]
when i used +1) before the * 2] i got correct numbers at value=0 & value=1

still the 3 and 4 doesn't work at all when i have * 2
Copy linkTweet thisAlerts:
@scragarFeb 13.2008 — that's because you don't have a 6th and 8th option.

if your just reading in values 1 through 4 you could just do:
[code=php]age[0].innerHTML=(numbers.selectedIndex+1) * 2;[/code]
Copy linkTweet thisAlerts:
@cepeslanauthorFeb 14.2008 — hmm is there a way to have let's say 8 options and hide some ? so they dont appear in the list
Copy linkTweet thisAlerts:
@scragarFeb 14.2008 — you could try 'display: none' as CSS to an option element, but that gives very unpredicable results in opera(strange extra empty options appear) and firefox(some options cause parts of a page to disapear in some cases, can crash). untested in IE. konqueror works wonderfully with it though ?

your proberly better of going for a javascript array:

[code=php]var n = new Array();
n[0]='2';
n[1]='4';
...
age[0].innerHTML=n[numbers.selectedIndex];[/code]
×

Success!

Help @cepeslan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...