Created
August 15, 2017 15:17
-
-
Save SteGriff/6851abc253f0256c64d87823023a7ae5 to your computer and use it in GitHub Desktop.
Custom HTML attributes in pure JavaScript
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
| <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