Skip to content

Instantly share code, notes, and snippets.

@darolo
Last active July 20, 2018 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darolo/cba5eda0421153f2dbb6618a9729db5d to your computer and use it in GitHub Desktop.
Save darolo/cba5eda0421153f2dbb6618a9729db5d to your computer and use it in GitHub Desktop.
Get options from element
function queryStringToJSON(string){
var result = {};
string.replace(/([^;&]+)[=:]+([^;&]+)/g, function(match, key, value, begin, fullstring){
result[key] = value;
});
return result;
}
function getOptionsFromElement(dataAtrribute, element){
var attr, ele;
if( typeof dataAtrribute == 'string' && typeof element == 'object' ){
attr = dataAtrribute;
ele = element;
} else if ( typeof dataAtrribute == 'object' ){
attr = 'data-options';
ele = dataAtrribute;
}
if( ele instanceof HTMLElement ) ele = $(ele);
else if( !(ele instanceof jQuery) ) return false;
return queryStringToJSON(ele.attr(attr));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment