data-set

Exemple 1

  1. <button data-toggle-id="calendar">
  2.   Show calendar
  3. </button>

  4. <form id="calendar" hidden>
  5.   mois de mai !
  6. </form>


  1.   document.addEventListener('click', function(event) {
  2.     let id = event.target.dataset.toggleId;
  3.     if (!id) return;

  4.     let elem = document.querySelector(`'#${id}'`);
  5.     elem.hidden = !elem.hidden;
  6.   });


https://jsbin.com/wagipuj/edit?html,js,output

Exemple 2

  1. <div id="menu">

  2.   <button data-action="save">Save</button>

  3.   <button data-action="load">Load</button>

  4.   <button data-action="search">Search</button>

  5. </div>


  1. class Menu {

  2.     constructor(elem) {

  3.       this._elem = elem;

  4.       

  5.     }


  6.     save() {

  7.       alert('saving');

  8.     }


  9.     load() {

  10.       alert('loading');

  11.     }


  12.     search() {

  13.       alert('searching');

  14.     }


  15.     onClick(event) {

  16.       let action = event.target.dataset.action;

  17.       if (action) {

  18.         this[action]();

  19.       }

  20.     };

  21.   }


  22.   new Menu(menu);

https://jsbin.com/dufunaf/5/edit?html,css,js,console,output