Reputation: 739
This piece of javacsript code (using jquery.treeview and jquery.cookie plugins) produces cookies named "tree_state_undefined".
I'm really not much into javascript, so anyone can tell me, how to get IDs of individual ul.tree elements in initialization?
thanks very much
$("ul.tree").treeview({
collapsed: true,
animated: "medium",
control:"#controller_" + $(this).attr('id'),
persist: "cookie",
cookieId: "tree_state_" + $(this).attr('id')
});
for the record, it's this plugin:
https://github.com/jzaefferer/jquery-treeview/blob/master/jquery.treeview.js
Upvotes: 1
Views: 81
Reputation: 38131
At the point in which you are running the code, the value of the this
variable is likely to be the window
, or, for some reason we can't see from the code you posted, the body
element.
The this
value changes according to how a function is invoked, see this section on Javascript Garden for more details. In your case, I suspect that if this code is run globally, or inside a $(document).ready(function () { ... });
block, this
will refer to the window
/document
.
Thus when you try to do $(this).attr('id')
, it is trying to get the id
attribute of the body, which is undefined. Then you concatenate that to a string, which is why you get a cookie called "tree_state_undefined".
Upvotes: 1
Reputation: 100195
you could try as:
$("ul.tree").each(function() {
var $ul = $(this);
$ul.treeview({
collapsed: true,
animated: "medium",
control:"#controller_" + $ul.attr('id'),
persist: "cookie",
cookieId: "tree_state_" + $ul.attr('id')
});
});
Upvotes: 1