2 Replies - 1266 Views - Last Post: 17 January 2009 - 04:38 PM

#1 rjbzzr400  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-December 07

Can't get values from a selection box for a JS script

Posted 17 January 2009 - 03:09 PM

Hi, I've written a piece of code and I need it to take the value and text from a selection box when a button is pressed, and perform a calculation on the value and add the text to a list. I have searched on how to do this, but I can't quite understand it.
I can't get the code that retrieves the value and text for the selection to work.

<form name=rewards2>
<select name=prizesDD2 id=prizesSel2>
  <option value=5>Apple</option>
  <option value=10>Banana</option>
  <option value=20>Pear</option>
  <option value=25>Orange</option>
</select>
<input type=button name=prizeBtn onclick="selectPrize2()" value="Buy">
</form>
<script>
function selectPrize2 () {

var i = document.rewards2.prizesSel2.selectedindex;   //
var wonPrize = document.rewards2.prizesSel2[i].text; //  Need to get this to work
var cost = document.rewards2.prizesSel2[i].value;	 //

	if (cost > totalTokens) {
		alert("You do not have enough tokens to buy that prize");
	}
	else if (cost <= totalTokens) {
		totalTokens=totalTokens-cost;
		alert("You bought "+wonPrize+" yay!")
		document.slots.banner.value=wonPrize;
		document.slots.tokens.value=totalTokens;	
	}
}
</script>



I've also tried make a multi dimensional array and set the values of the selection box to indexes to select a position in the array but I can't get that to work either.
var cost=prizes[document.rewards.prizesDD.value][0];
var wonPrize=prizes[document.rewards.prizesDD.value][1];



If any one could let me know what I'm doing wrong I would greatly appreciate it :)
Thanks :^:

This post has been edited by rjbzzr400: 17 January 2009 - 03:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can't get values from a selection box for a JS script

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5205
  • View blog
  • Posts: 13,951
  • Joined: 18-April 07

Re: Can't get values from a selection box for a JS script

Posted 17 January 2009 - 04:18 PM

In a select box like this the options form an array that belong to the select box. You use this array to get at the selected option and its value. Lastly remember that javascript is case sensitive so it is selectedIndex and not selectedindex.

So you need to fix the lines like so....

// Notice capital "I" for Index.

var i = document.rewards2.prizesSel2.selectedIndex;
var wonPrize = document.rewards2.prizesSel2.options[i].text; 
var cost = document.rewards2.prizesSel2.options[i].value; 



Once youf ix this then you should be good to go. Enjoy!

"At DIC we be option selecting code ninjas... yeah our cars are full of options, all selected using getElementById. Drove the dealership nuts." :snap:

This post has been edited by Martyr2: 17 January 2009 - 04:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 rjbzzr400  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-December 07

Re: Can't get values from a selection box for a JS script

Posted 17 January 2009 - 04:38 PM

Yay I was almost right :D
Thanks alot :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1