Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created August 15, 2017 15:17
Show Gist options
  • Select an option

  • Save SteGriff/6851abc253f0256c64d87823023a7ae5 to your computer and use it in GitHub Desktop.

Select an option

Save SteGriff/6851abc253f0256c64d87823023a7ae5 to your computer and use it in GitHub Desktop.
Custom HTML attributes in pure JavaScript
<button type="button"
submit-item
data-url="/Controller/Action"
data-item-code="ABC123">Submit ABC123</button>
<script>
//Helper to get data-items without jQuery
Object.prototype.data = function(x)
{
if (this.dataset) { return this.dataset[x] }
return [];
};
(document.querySelectorAll('[submit-item]')[0]).onclick = function (e) {
e.preventDefault();
console.log(e);
console.log(this);
var url = this.data("url");
var itemCode = this.data("item-code");
var data = {
itemCode: itemCode
};
UpdateContent(url, data, function afterSend(result) {
//Act on response
console.log("Response", result);
});
};
function UpdateContent(url, data, callback)
{
//Submit to server
console.log("Send", data, "to", url);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment