dav
dav

Reputation: 9267

dynamically get jquery object's value

I have jquery object myObject like this(result of console.log())

Object { Product1 {...}, Product2 {...}, Product3 {...} }

and each of these products' pattern is like this Object { id="1", name="somename" }

I want to get the value of the name, when I change the radio button, e.g. if the first radio is checked I should get value of myObject.Product1, if second - myObject.Product2. When I e.g. try this console.log(myObject.Product1), it normally outputs Object { id="1", name="somename" }, but when I want to do it dynamically like

var number = getRadioValue(); // for example returns 1 
var productName = 'Product'+number;

then it outputs myObject.productName as undefined.

Thanks

Upvotes: 0

Views: 2428

Answers (1)

wirey00
wirey00

Reputation: 33661

If you are passing in a variable.. you should use the object[property] syntax instead of the . notation

myObject[productName]

Upvotes: 3

Related Questions