I’m attempting to reference the discount_code property from within the object, but I keep getting the following error. How can I access discount_code?
Error:
Uncaught TypeError: Cannot call method ‘val’ of undefined
HTML:
<input type="text" name="txt_discount_code" value="12345" />
JS:
var cart = {
transaction: {},
discount_code: $('input[name=txt_discount_code]'),
get_cart_items_params: {
page: 'checkout',
s_method: 'get-cart-items',
txt_discount_code: this.discount_code.val()
// txt_discount_code: cart.discount_code.val()
}
};
Like
ShankarSangolisaid, you can’t access the object before it’s defined.You have to break up the declaration of
cartinto 2 parts:Or just put
discount_codeinto a variable: