Skip to content

Instantly share code, notes, and snippets.

@westonwatson
Last active December 15, 2015 22:59
Show Gist options
  • Save westonwatson/5336571 to your computer and use it in GitHub Desktop.
Save westonwatson/5336571 to your computer and use it in GitHub Desktop.
similar to my datafill.js gist, uses data attributes instead of DOM id's
datatags = function (dataObject,parentName){
if(typeof(dataObject) == 'object') {
for(var item in dataObject) {
var value = dataObject[item];
var targetId = parentName ? (parentName + "-" + item) : (item);
targetId = '[data-'+targetId+']';
var currentSelection = $(targetId);
if (typeof(value) == 'string' || typeof(value) == 'number' || typeof(value) == 'boolean'){
if (typeof(value) == 'object' && value != null) datatags(value,item);
if (currentSelection.is(':input')){
currentSelection.val(value);
}else{
currentSelection.html(value);
}
}
}
}
};
/*
<!--example-->
<div data-user-name></div><div data-user-address></div>
<script>datatags({user:{name:"Weston",address:"1234 ThisStreet Dr."}});</script>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment