I\'m using the jsTree jQuery plugin with the checkbox theme. Does anyone know how to get the selected values with a form post?
Thank you!
var selectedElmsIds = [];
$("#jstree2").find("li").each(function(i, element) { //also includes the ones below 'undetermined' parent
if ($(this).attr("aria-selected") == "true") {
selectedElmsIds.push($(this).attr('id'));
}
});
console.log(selectedElmsIds);
Have you got your answer ? If not, here is one that appears in the jstree google groups
function submitMe(){ var checked_ids = []; $("#server_tree").jstree("get_checked",null,true).each (function () { checked_ids.push(this.id); }); doStuff(checked_ids);
In the last version (3.0), the API was changed.
If you need just array of selected IDs (like in examples in this node), it is now very easy:
var selectedElmsIds = $('#tree').jstree("get_selected");
If you need to iterate over the selected elements, you just need to pass additional "true" parameter.
var selectedElmsIds = [];
var selectedElms = $('#tree').jstree("get_selected", true);
$.each(selectedElms, function() {
selectedElmsIds.push(this.id);
});
With jQuery
you can simply do:
$('.jstree-checked,.jstree-undetermined').each(function(){
var rawCheckedID = $(this).find('a').attr('id');
});
This will get the undetermined and checked at the same time. soumya's solution above may be more efficient.
$(document).ready(function(){
var jsfields = $('#myTree').jstree('get_selected');
$('.jsfields').val(JSON.stringify([jsfields]));
})
<input type="hidden" class="jsfields" value=""/>
Change the value of $('#myTree')
to you respective tree, this is best working for me in ajax call. slight modification may be needed to populate input field of simple form.
//click button show nodes checked
$(document).on('click','#showme',function() {
var checked_ids = [];
var checked_ids_meta = [];
$('#demo_0').jstree("get_checked",null,true).each(function(i, e){
checked_ids.push($(this).data("param")); // json metadata
checked_ids_meta.push($(this).attr("href")); // json attr
});
console.log(checked_ids)
console.log(checked_ids_meta)
});