How to get the node attributes in jstree JSON data
11,059
You only need to get the id of selected element and then get the attribute of that element:
$(function () {
$('#jstree').jstree({ 'core' : {
'data' : [
{"id":"parent","parent":"#","text":"parent"},
{"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}},
{"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"},
{"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"},
{"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"},
{"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"}
]
} }).on("select_node.jstree",
function(evt, data){
var node_id = (data.node.id); // element id
var description = $("#"+node_id).attr("description"); // get value of element attribute
$('#data').html(description);
}
);
});
Note that not all of the element has description
attribute.
Author by
coffeeak
Updated on June 04, 2022Comments
-
coffeeak almost 2 years
I want to get the attributes of each node in my tree. After looking online, I found a way to do it but it doesn't work. The error is (at the line of attr("description") :
Uncaught TypeError: Cannot read property 'obj' of undefined
Here is my code:
jQuery(document).ready(function() { var $ = jQuery; $('#jstree').jstree({ 'core' : { 'data' : [ {"id":"parent","parent":"#","text":"parent"}, {"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}}, {"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"}, {"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"}, {"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"}, {"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"} ] } }) .on("select_node.jstree", function(evt, data){ $('#data').html(data.rslt.obj.attr("description")); } ); });
-
Oki Erie Rinaldi about 8 yearsdid you mean attribute values or attribute names?
-