简体   繁体   中英

scoping html data with js or jquery

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.

var type = JSON.parse($("#"+node_id).attr("data-jstree")).type

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM