Source Code:
(back to article)
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> </head> <body> <div id="buttonList"> <button data-action="save">Save</button> <button data-action="load">Load</button> <button data-action="search">Search</button> </div> <script> class List { constructor(buttonElem) { this._buttonElem = buttonElem; buttonElem.onclick = this.onClick.bind(this); // (*) } save() { alert('Saving...'); } load() { alert('Loading...'); } search() { alert('Searching...'); } onClick(event) { let action = event.target.dataset.action; if(action) { this[action](); } }; } new List(buttonList); </script> </body> </html>
Result:
Report an issue