Exemple 1
- <button data-toggle-id="calendar">
- Show calendar
- </button>
- <form id="calendar" hidden>
- mois de mai !
- </form>
- document.addEventListener('click', function(event) {
- let id = event.target.dataset.toggleId;
- if (!id) return;
- let elem = document.querySelector(`'#${id}'`);
- elem.hidden = !elem.hidden;
- });
Exemple 2
<div id="menu">
<button data-action="save">Save</button>
<button data-action="load">Load</button>
<button data-action="search">Search</button>
</div>
class Menu {
constructor(elem) {
this._elem = elem;
}
save() {
alert('saving');
}
load() {
alert('loading');
}
search() {
alert('searching');
}
onClick(event) {
let action = event.target.dataset.action;
if (action) {
this[action]();
}
};
}
new Menu(menu);
https://jsbin.com/dufunaf/5/edit?html,css,js,console,output