Scott Yu - builds stuff
Scott Yu - builds stuff

Reputation: 11763

Stumped on JS Tree... how to get the ID of currently selected <LI>?

I wish JStree had more examples of peforming AJAX actions when deleting a node, etc. Like how does it transfer the ID of selected node and pass that as a parameter? Spent a few hours and can't figure this one out.

<li id="10" class="open"><a style="" class="clicked" href="#"><ins>&nbsp;</ins>fff</a> </li> 
<li id="1" class="open"><a href="#"><ins>&nbsp;</ins>111</a> </li> 
<li id="2" class="open"><a href="#"><ins>&nbsp;</ins>aaa</a> <ul> 
<li id="3" class="open"><a href="#"><ins>&nbsp;</ins>Wonderful</a> <ul> 
<li id="9" class="open last"><a href="#"><ins>&nbsp;</ins>bbb</a>
 </li> 
 </ul> 

So if I do something like a rename, how do I get the ID in the LI tag of selected node like the node_id?

$("#tree2").tree({
    callback : {
    onrename : function (NODE, TREE_OBJ) {
        alert(node_id); 
    }

} });

Any help appreciated!

Upvotes: 2

Views: 5057

Answers (3)

Aditya P Bhatt
Aditya P Bhatt

Reputation: 22081

I was struggling for same and found a simplest way as below for key and value:

alert(node.id + ' ' + node.text);

Full Example Below:

HTML Code (have to assign id for each li either parent or child element):

<li id="<?php echo $result['FILTER_ID']; ?>">

Complete JS Code below:

var nodes = $('#tt').tree('getChecked');
    var s = '';
    for(var i=0; i<nodes.length; i++){
        if (s != '') s += ',';
        s += nodes[i].id + ' ' + nodes[i].text;
    }
    alert(s);

Upvotes: 1

lykm
lykm

Reputation: 15

You should use node.id instead.

Upvotes: 1

Pointy
Pointy

Reputation: 413826

The plugin passes you the node involved; have you tried simply

alert($(NODE).attr('id'));

?

Upvotes: 4

Related Questions