user2514244
user2514244

Reputation:

Using this on objects

So I'm making a game, but I'm having trouble selling items. I made the object

var price = {
sword:3,
fish:1
}

And once you click on something with class "item", it should tell you the price Except It isn't working

$(".item").click(function(){
alert(price.(this.id))
});

Can someone help me?

Upvotes: 0

Views: 104

Answers (3)

user2681150
user2681150

Reputation:

access by

 alert(price[this.id]);

Upvotes: 1

Anton Krutikov
Anton Krutikov

Reputation: 56

Replace

    alert(price.(this.id))

To

    alert(price[this.id])

You can't access object properties by dynamic names from dot notation, use subscript notation.

Upvotes: 3

Rituraj ratan
Rituraj ratan

Reputation: 10388

$(".item").click(function(){
alert(price[this.id]);
});

Upvotes: 6

Related Questions