0

I'm trying to work with HTML5 data-* attributes. However I'm using the EasyUI framework and it's being problematic.

HTML5 defines options are set as follows: <div data-options="{region:'north', title:'North Region', border:true}">

But EasyUI enforces they are set like (no curly brackets): <div data-options="region:'north', title:'North Region', border:true">

Is there a way to access the attribute object without writing my own parser function? If I have to I must but I figure there must be a better way.

Thanks for any help.

user1389920
  • 403
  • 1
  • 6
  • 12

3 Answers3

1

Looking at the source, there is a method that does this.

$.parser.parseOptions(element);

Demo with just the method extracted: http://jsfiddle.net/FnJAE/

Note: this method is not documented, therefore it is subject to change without notice.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
0

If you know it will always be formatted as an object you could just:

jsonData = JSON.parse("{" + element + "}");
jeremy
  • 4,294
  • 3
  • 22
  • 36
0

Just FYI, you really dont have to do all that parsing. Though your HTML does need some change in quotes: See Reference jQuery.Data() HTML5

HTML

<div id="Bob" data-options='{"region":"north", "title":"North Region", "border":"true"}'></div>

Script

console.log($("#Bob").data("options").title);
// Will return String "North Region"

console.log($("#Bob").data("options").region);
// Will return String "north"

console.log($("#Bob").data("options").border);
// Will return String "true"

console.log($("#Bob").data("options")
// will return a JSON Object but it is EASY to convert to Array
var eleData = $("#Bob").data("options"),
    eleArray = new Array();
for (x in eleData) { eleArray[x] = eleData[x]; }
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
  • I agree, fix the data to be correct json, but that isn't an option without modifying the easyui core (which wouldn't really be that difficult, tbh). http://www.jeasyui.com/documentation/index.php# – Kevin B Nov 20 '12 at 20:11
  • I still say jQueryUI is 100x better and much more beneficial. – SpYk3HH Nov 20 '12 at 20:18