JSFiddle - React, Tailwind, and code Playground

by cmruser

HTML

<a data-onclick="print" data-target="test">click</a>
<a data-onclick="toggle">click</a>
<a data-click="print">click</a>

JavaScript

var events = {
	click: {
  	print: function() {
    	if($(this).attr('data-target')) {
				alert('yes');
 			} else {
 				alert('no');
 			}
    }
  }
};

$('[data-onclick]').on('click', function(e) {
	e.preventDefault();
  e.stopPropagation();
  
  if(e.type in events && $(this).data('on' + e.type) in events[e.type]) {
    events[e.type][$(this).data('on' + e.type)].apply(this, arguments);
  }
});

function _init() {
	var _events = {
    click: {
      print: function() {
        alert('print inside');
      }
    }
  };

  $('[data-click]').on('click', function(e) {
    e.preventDefault();
    e.stopPropagation();

    if(e.type in _events && $(this).data(e.type) in _events[e.type]) {
        _events[e.type][$(this).data(e.type)].apply(this, arguments);
    }
  });
}

_init();