I am willing to use the jquery treeview. I have categories and subcategories to choose for an item and I would like to display them in a treeview. I would like then to get the clicked value.
for the moment I am working on something of the kind of :
<ul id="treeview">
<li>group1a
<ul>
<li>group11 </li>
</ul>
</li>
<li>group2 </li>
<li>group3 </li>
<li>group4 </li>
<li>group5 </li>
</ul>
and I tried this script, but the click function throw me an error.
<script type="text/javascript">
$().ready(function () {
$("#treeview").treeview();
});
$("#treeview").click(function (e) {
e.target.addClass("selected");
});
</script>
I am a very big beginner to this Jquery way of handling things, so I assume I am missing some important point somewhere… thanks for your help..
The
addClassis an jQuery method, whilee.targetis not a jQuery object. You need to enclose it in$():Your code won’t work anyways, as the click event is bound only to the
#treeviewelement, and when that element fires,e.targetwill always be the#treeviewelement. What you’re looking for is probably something like this:This binds the click function to all
lielements, and when one of them is clicked, it adds the “selected” class to that element.Probably you want to also allow deselecting of objects, so you should use
toggleClassinstead ofaddClass. If you want to allow selecting of only one object, you could use this:Hope this helps.