1

This is my html ( in twig template )

<li id="{{folder.id}}" data-jstree='{"icon":"glyphicon glyphicon-tags", "type":"folder"}' ><a href="#">{{folder.name}}</a>

I am trying to get the value of 'type' from 'data-jstree'.

I tried using

var node_id = ref.get_node(sel[i]).id;
var type = $("#"+node_id).attr("data-jstree");

but that gives me this : {"icon":"glyphicon glyphicon-tag", "type":"tag"} and i only need the value of type.

Thanks in advance.

3
  • 1
    $("#"+node_id).data("jstree").type Commented Jul 18, 2017 at 14:22
  • 1
    var type = JSON.parse($("#"+node_id).attr("data-jstree")).type; Commented Jul 18, 2017 at 14:23
  • 3
    Possible duplicate of Store JSON object in data attribute in HTML jQuery Commented Jul 18, 2017 at 14:23

2 Answers 2

4
var type = JSON.parse($("#"+node_id).attr("data-jstree")).type
Sign up to request clarification or add additional context in comments.

Comments

2

you need to parse the string into json. do something like this:

var node_id = ref.get_node(sel[i]).id;
var type = $("#"+node_id).attr("data-jstree");
type = JSON.parse(type).type;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.