Get Selected Jstree node values JQuery -


hi have problem getting data of selected node of jstree model.

<script type="text/javascript">     $('#preview').on("changed.jstree", function (e, data) {         console.log(data.selected);         console.log(data.selected.attr("text"));     }); </script> 

the first console log shows me "[js1_1]" or "[js1_2]" depending on selected node. second log "undefined not function" ;/ have tried many different ways failed achieve getting node text(title) or other info

i send list of models json , looks this:

public class jstreemodel    public property text string    public property icon string    public property id string    public property pid string    public property parentid string    public property status integer end class 

anyone has solution ?

update jstree code

<script type="text/javascript"> $(document).ready(function () {      $('#whenremoving').toggle($('.removecheckbox').is(":checked"));     $('#whenadding').toggle(!$('.removecheckbox').is(":checked"));      $('#preview').jstree({         'core': {             'data': {                 'url': '/treetest/treepreview/',                 'data': function (node) {                     return node;                 }             }         }     }).bind("loaded.jstree", function (event, data) {         $(this).jstree("open_all");     }) }); </script> 

when add jstree can see selected name node in html div

.on('changed.jstree', function (e, data) {         var i, j, r = [];         (i = 0, j = data.selected.length; < j; i++) {             r.push(data.instance.get_node(data.selected[i]).text);         }         $('#event_result').html('selected: ' + r.join(', '));     }) }); 

instead of

data.selected.attr("text") 

try this

data.selected.text() 

you can selected jstree node text :

console.log($("#preview").jstree("get_selected").text()); 

or bind select_node.jstree shown

.bind("select_node.jstree", function (node, ref_node) {         var = $.jstree._focused().get_selected();     } 

Comments