0

Inside a JS file I have this:

task_categories = new CategoryTree();

$('input:radio[name=what_tasks]').click(function () {
    var where = getLocation();
    taskList(where.project, task_categories.selected_elements, where.page);
});

Firefox debugger won't complain me trying to access task_categories but unfortunately, task_categories will be undefined.

How can I make this work? Thanks!

SrgHartman
  • 651
  • 2
  • 8
  • 23
  • 4
    show **CategoryTree** definition – letiagoalves Mar 13 '13 at 14:37
  • `var task_categories = new CategoryTree();` ? – Ramunas Mar 13 '13 at 14:39
  • 3
    also, show what tasklist is doing; one more thing, using `where` as a variable name is a big no-no, it is a reserved word in javascript. – Rob M. Mar 13 '13 at 14:39
  • task_categories can be defined elsewhere, but the assignment is probably undefined which is why you would get an error. if you try `task_categories = {}` the error should go away. (although `.selected_elements` will be undefined – Shanimal Mar 13 '13 at 14:40
  • Um, in the code sample, it should be fine. Is that exactly how the code looks like? Is the variable changed before the click event? – epascarello Mar 13 '13 at 14:40
  • task_categories won't be accessed until the click event happens. Do you set it to undefined at a later point in the code? – Ben McCormick Mar 13 '13 at 14:40
  • 1
    perhaps you overwrite the (possibly accidentally global) variable later? – John Dvorak Mar 13 '13 at 14:40
  • I fixed it by having this.selected_elements in CategoryTree instead of var selected_elements. I still don't understand when to use this or var I'm affraid. – SrgHartman Mar 13 '13 at 14:42
  • 2
    @SrgHartman: Then [Javascript: Do I need to put this.var for every variable in an object?](http://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object/13418980#13418980) might help you – Bergi Mar 13 '13 at 14:49

0 Answers0