Last active
July 20, 2018 13:23
-
-
Save darolo/cba5eda0421153f2dbb6618a9729db5d to your computer and use it in GitHub Desktop.
Get options from element
This file contains 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 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