var options = { selector: '.tabbed' }; (function(options) { var $containers = $(options.selector); $containers.each(function() { var $container = $(this), $tabs = $container.find('.tabs').attr('data-instance', new Date().getTime()), // adding data-instance for events to work correctly when multiple instances of this tabbed thingy are issued. $contentItems = $container.find('.content'); $tabs.on('click', function(event) { // simple event delegation if(event.target.nodeName === 'LI') { var tabId = $(event.target).attr('data-tab'); // close all previously opened tabs $tabs .find('[data-open=true]') .attr('data-open', 'false'); // open clicked tab $tabs .find('[data-tab='+tabId+']') .attr('data-open', 'true'); // hide previously open tab content $contentItems .find('.item') .attr('data-open', 'false'); // open current tab content $contentItems .find('[data-item=' + tabId + ']') .attr('data-open', 'true'); } }); }); })(options);