Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Latest commit

 

History

History
36 lines (26 loc) · 728 Bytes

README.md

File metadata and controls

36 lines (26 loc) · 728 Bytes

Data-* Click Event, Sample Code

Sample code using data-* attributes to call click functions.

var funcs = {
	click1: function () {
		console.log('click1!!!');
	},
	click2: function () {
		console.log('click2!!!');
	}
};

function executeFunctionByName(functionName, context) {
	var args = [].slice.call(arguments).splice(2);
	var namespaces = functionName.split(".");
	var func = namespaces.pop();
	for (var i = 0; i < namespaces.length; i++) {
		context = context[namespaces[i]];
	}

	context[func].apply(this, args);
}


$(function () {
	$('a[data-trigger]').on('click', function () {
		var action = $(this).data('trigger');
		executeFunctionByName(action, funcs);
	});
});