/    Sign up×
Community /Pin to ProfileBookmark

Multiple values on option list

Hey all, I’m a bit new to JavaScript so bear with me!

My main Website uses the Paypal buy it now buttons which i have found i can manipulate by using drop down menus.

What i Want to do now is capture 2 values from each option (one to alter the despcription one to alter the price) but i’m failing to succeed.

This is what I have (edited out the non relevant bits)

[I]the code bit[/I]

[code]
function hondachainselect()
{
document.hondachain.item_name.value = document.hondachain.hondachainproduct.value + document.hondachain.hondachain_bike.value

}

[I]the html form bit[/I]

<form target=”paypal” action=”https://www.paypal.com/cgi-bin/webscr” method=”post” name=”hondachain”>

<b> Please Choose your colour:<br></b>
<select name=”hondachain_bike” onChange=”hondachainselect()”>
<option value=” 0 “>Choose Bike</option>
<option value=”CRF50 04-06″ >CRF50 04-06 £19.99</option>
<option value=”CRF70 04-06″ >CRF70 04-06 £19.99</option>
<option value=”CRF100 04-06″ >CRF100 04-06 £29.99</option>

</select>

<br><font size=”4″><b>from £19.99</b><br></font>
<input type=”hidden” name=”hondachainproduct” value=”Chain & Sprocket Kit – “>

<input type=”image” src=”https://www.paypal.com/en_GB/i/btn/x-click-but22.gif” border=”0″ name=”submit” alt=”Make payments with PayPal – it’s fast, free and secure!”>

<input type=”hidden” name=”item_name”>
<input type=”hidden” name=”amount”>

</form>
[/code]

with that the “item_name” comes out correctly but I’ve given up how to get a 2nd value from the options to assign to “amount”

Many thanks in advance for your help!!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 12.2007 — [I]value[/I] attribute returns a string, not a number, thus the + operator will proceed a concatenation, tot a Math add. Use Number() method:
<i>
</i>document.hondachain.item_name.value = Number(document.hondachain.hondachainproduct.value) + Number(document.hondachain.hondachain_bike.value)


But... do you need a math addition? Or what?
Copy linkTweet thisAlerts:
@FluffyauthorNov 12.2007 — the item_name value returns exactly the string i want it to

What i need to know id can the options carry a 2nd value that i can then pass to the "amount" input??

Thanks All
Copy linkTweet thisAlerts:
@FluffyauthorNov 12.2007 — SOLVED!!!



with an array like this -



function hondachainselect()

{

var bike = new Array(), bi=0;

bike[bi++] = {model:'CRF50 04-06', price:'19.99'};

bike[bi++] = {model:'CRF70 04-06', price:'19.99'};

bike[bi++] = {model:'CRF100 04-06', price:'29.99'};

bike[bi++] = {model:'CR85 R 03-04', price:'34.99'};

var bikevalue = document.hondachain.hondachain_bike.value;

document.hondachain.item_name.value = document.hondachain.hondachainproduct.value + bikevalue;

for (var bi=0;bi<bike.length;bi++)

{

if (bike[bi].model == bikevalue)

{

document.hondachain.amount.value = bike[bi].price;

break;

}

}

}
Copy linkTweet thisAlerts:
@yaronsh1Nov 12.2007 — I usally put the additional data as custom attributes on the option element and then retrieve it from there.

var selectedOption=document.hondachain.hondachain_bike.options[document.hondachain.hondachain_bike.selected];

selectedOption will reference the option the user selected.

If you create the option like this:

<option value="CRF50 04-06" price="£19.99">CRF50 04-06 £19.99</option>

then you can get the price like that:

var price = selectedOption.price;

Another comment:

Make sure to validate the price again on the server as it can be easily change in the client side to 0£ :-(
×

Success!

Help @Fluffy 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.11,
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,
)...