Joe Consterdine Joe Consterdine - 11 months ago 93
Javascript Question

Undefined Javascript Error On Click

not sure why I'm getting this error when I attempt to click on the button.

Uncaught TypeError: Cannot read property 'on' of undefined




(function() {
var itemTracker = {
// init
init: function() {

// cacheDom
cacheDom: {
inputAdd: $('#inputAdd'),
submitAdd: $('#submitItem')

// item
item: {
text: this.inputAdd.value,

// Bind Events
bindEvents: function() {
this.submitAdd.on("click", addItem);

// Add item
addItem: function() {

// Remove item

// Edit Item

// Complete Item

// Uncomplete Item




Answer Source

You are accessing the property in a wrong way,

bindEvents: function() {
  this.cacheDom.submitAdd.on("click", addItem);

When seeing your code, I can tell cacheDom is an object which holds submitAdd as a property in it.


item: {
 text: this.inputAdd.value,

This part of the code wont work as expected. You can write it as a getter to solve your problem.

get itemText() {
 return this.cacheDom.inputAdd.value,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download