/    Sign up×
Community /Pin to ProfileBookmark

Getting the value of a drop down

I’m trying to get the value from the selected option in a drop down and multiply it by another. The values of this particular drop down are numbers, but I keep getting NaN.

This is the form:
<select size=”1″ name=”number” tabindex=”4″>
<option value=”1″ selected=”selected”>Choose Number</option>
<option value=”1″>1</option>
<option value=”2″>2</option>
</select>

Here’s the script I’m trying to use:

function calcTotal5(){
var seating = document.FrontPage_Form1.price.value;
var subscriptions = document.FrontPage_Form1.number.value; // problem is here
var calculate = seating * subscriptions;
document.FrontPage_Form1.TOTAL.value = calculate;
}

I also tried this:
var subscriptions = document.FrontPage_Form1.number.options[document.FrontPage_Form1.number.selectedIndex].value;

I’m much more at home with PHP, but have to use javascript for this. The problematic line is the one I commented on. If I comment that entire line out and where the multiply is, replace subscriptions with an integer, it works fine. I’m just not grabbing the value from the drop down ‘number’ correctly. When I get the value of subscriptions, it’s ‘undefined’.

Any help would be appreciated, thank you

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisMay 23.2007 — Forms are always strings. Since you cannot multiply strings, you have to convert them to numbers first.
Copy linkTweet thisAlerts:
@stinkysGTIauthorMay 23.2007 — They were both number values.

I found the problem though, just now. I changed the name of the drop from 'number' to 'subscriptionNumber' and now it works. I guess it was because number is a reserved js name, but I wasn't getting any errors regarding that.

Thank you for the response
×

Success!

Help @stinkysGTI 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...