I have a habit of using lots of data tags, and then reading them one at a time in jQuery e.g.
*html*
<span data-group="1" data-item="2" data-user="abc" data-type="4" . . . . . </span>
*jQuery*
getItemSelected = function($in){dataItem=$in.data("item");}
But I'm wondering if there may be a better way than creating an ever increasing number of data tags, by using one tag with json as the content. But so far all my attempts at this have failed.
I'm thinking something like the following:
*html*
<span data-options='[{"group":"1","item":"2","user":3". . . .}]'></span>
*jQuery*
getOptions = function($in)
{
dataOptions=jQuery.parseJSON( $in.data("options") );
alert(dataOptions === "group");
}
But every variation I have tried so far simply comes up as undefined. What is the correct way of doing this?
$in.data("options")[0].group. You've made your JSON represent an array by using[and]. Also, jQuery'sdatafunction will automatically parse JSON indata-attributes<span data-options='{"group":"1","item":"2","user":3". . . .}'></span>