Accessing selected node of richfaces tree from Javascript

java, javascript, jsf, richfaces, tree

Solution

The answer is that the javascript code should be on the node level instead of the tree level.

<head>
<script type="text/javascript">
function documentClicked(nodeRef)
{
    alert("Node id is "+nodeRef);
}

</script>
</head>


    <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}"  
        var="document" >

        <rich:treeNode onclick="documentClicked('#{document.id}')">

            <h:outputText value="#{document.friendlyName}" />

        </rich:treeNode>

Problem

This should be a very simple question. I have a richfaces tree that is rendered using JSF. When the user clicks on a node I want a javascript function to run. Nothing more nothing less. No redirects, no re-submit, no-rerender, no Ajax. Just plain old Javascript. I have seen the onselected attribute of the tree and it indeed fires a Javascript method. But of course I want to know which node was clicked. Here is what I have so far ``` <head> <script type="text/javascript"> function documentClicked(nodeRef) { alert("Node is "+nodeRef); } </script> </head> <rich:tree switchType="client" value="#{ajaxDocumentTree.rootNode}" var="document" onselected="documentClicked()" > <rich:treeNode iconLeaf="../images/tree/doc.gif" icon="../images/tree/doc.gif"> <h:outputText value="#{document.friendlyName}" /> </rich:treeNode> ``` But this does not work because nodeRef is undefined. I expected that the first argument of the callback would be the selected node but this is not the case. So the question is this: How do I fire a Javascript function with the selected node from a richfaces tree?

Original source