3

I'm stuck with a weird issue!

I have this code:

<script>
  window.PRICES = {
    'Gold' : { 10: 299, 20: 400, 100: 1745, },
    'Coins': { 10: 300, 40: 355, 1000: 30000, },
  };
</script>
<script>
function pick(i,n) {
  document.getElementById("Item").innerHTML = i+" "+n;
  document.getElementById("Price").innerHTML = (window.PRICES)[i][n];
}
</script>

The function pick is called by a select's onchange event:

<select name="Gold" onchange="pick(this.value,this.name)">
  <option value="10">10 Gold</option>
  <option value="20">20 Gold</option>
  <option value="100">100 Gold</option>
</select>

But when I change the value I get:

"Uncaught TypeError: Cannot read property 'Gold' of undefined"

BUT window.PRICES isn't UNDEFINED! Whats wrong?

PS: I'm able to call pick('Gold','10') using GC console!

3
  • Put a console.log(i, n) in your event handler and verify that the values are really what you want them to be. Dec 2, 2012 at 19:30
  • wrong order of arguments, change to pick(this.name, this.value)
    – chipmunk
    Dec 2, 2012 at 19:31
  • 1
    To not mix up such things give proper names to your arguments in the function, make it function pick(name, value) {} Dec 2, 2012 at 19:33

1 Answer 1

7

The order of the arguments of your onchange handler is switched. It should be pick(this.name, this.value), since you are accessing the PRICES object first by name, and only then by value, just like in your example pick('Gold', '10').

1
  • 2
    @user1663047 : I would suggest you changing function pick(i,n) to function pick(productType,amount) too, so you don't have to remember/guess which is which.
    – Roimer
    Dec 2, 2012 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.