Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign Click Events to < li > in JQuery Treeview

I am using the JQuery Treeview plugin to display some data. Basically, I would like to add a click event to the child < li > elements that copies their innerhtml into another div on the page. I have not been able to assign a click event to these < li > elements however.

Hoping someone has tread this ground before and can provide some help.

Thanks.

like image 794
WorkingWeb Avatar asked Jul 03 '09 20:07

WorkingWeb


3 Answers

Using the markup from the example at http://docs.jquery.com/Plugins/Treeview:

$("span.file, span.folder", "#example li")
    .click(function() { alert($(this).text()); });

works. Handling the click on the LI items themselves captures branch contractions and expansion.

like image 62
Joe Chung Avatar answered Oct 19 '22 05:10

Joe Chung


You can use .live construction. For me it works:

$('li','ul#menu').live('click', function(){
    alert('Click event');    
});
like image 39
Vitaliy Shuruta Avatar answered Oct 19 '22 04:10

Vitaliy Shuruta


<li onclick="selectNode(event, this);" id="${node2.nodeId}" class="closed">
    <span class="folder"> ${node2.name} </span>
</li>




function selectNode(event, nodeHtmlEl) {
    // IE
    if ($.browser.msie) {
        window.event.cancelBubble = true; 
    }
    if(event.stopPropagation) { 
        event.stopPropagation();
    }
    alert("selectNode ID: " + $(nodeHtmlEl).attr("id"));
}
like image 29
Ilya Zadorozhny Avatar answered Oct 19 '22 03:10

Ilya Zadorozhny