Last active
August 16, 2018 15:43
-
-
Save brynner/c8fb3cd785ec9c11c4bcb6b608548810 to your computer and use it in GitHub Desktop.
Add data to HTML according to object's property name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Add data to HTML according to object´s property name | |
| function addContentFromDataToHtml(object, dataSet) { | |
| recursiveObject({object: object, callback: addContentFromDataToHtmlCallback, dataSet: dataSet}); | |
| } | |
| function recursiveObject(recursiveParams) { | |
| if (recursiveParams.parentsItem) { | |
| recursiveParams.parentsItem = recursiveParams.parentsItem+'.'; | |
| } else { | |
| recursiveParams.parentsItem = ''; | |
| } | |
| if (typeof(recursiveParams.object) == 'object') { | |
| for(var item in recursiveParams.object){ | |
| recursiveObject({object: recursiveParams.object[item], parentsItem: recursiveParams.parentsItem+item, callback: recursiveParams.callback, dataSet: recursiveParams.dataSet}); | |
| recursiveParams.callback(recursiveParams.parentsItem+item, recursiveParams.object[item], recursiveParams.dataSet); | |
| } | |
| } | |
| } | |
| function addContentFromDataToHtmlCallback(key, value, dataSet) { | |
| $((dataSet?'[data-set="'+dataSet+'"]':'')+' [data-content="'+key+'"]').text(value); | |
| $((dataSet?'[data-set="'+dataSet+'"]':'')+' [data-content="'+key+'"]').val(value); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div data-set="group-name"> | |
| <div data-content="propertyOne"></div> | |
| <div data-content="propertyTwo.sub.sub.sub"></div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getSomething(callbackGetSomething); | |
| function callbackGetSomething(dataObject) { | |
| addContentFromDataToHtml(dataObject 'group-name'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getSomething(callback) { | |
| var apiEndpoint = ''; | |
| $.get(apiEndpoint, null, function(apiResult) { | |
| callback(apiResult); | |
| }).fail(function(error) { | |
| return error; | |
| }); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/558445/dynamically-fill-in-form-values-with-jquery/28303108#28303108