Skip to content

Instantly share code, notes, and snippets.

@grayghostvisuals
Created June 5, 2014 16:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grayghostvisuals/55c225c9a4c6a9fd7d67 to your computer and use it in GitHub Desktop.
Save grayghostvisuals/55c225c9a4c6a9fd7d67 to your computer and use it in GitHub Desktop.
Passing Arguments to Event Functions
/**
* @about
* Binding arguments to a function passed through
* an event listener as a named function.
*
* @reference
* http://jsfiddle.net/toddmotto/D3tgu
*/
// Widely Supported : jQuery
// =========================================
function toggleNav(event, cname) {
$(this).toggleClass(cname);
}
$('.menu').on('click', function() {
toggleNav.call(this, event, 'active');
});
// Needs Bind Polyfill - IE 7/8 : Vanilla JS
// =========================================
var menu = document.querySelector('.menu');
function toggleNav(event, cname) {
this.classList.toggle(cname);
}
menu.addEventListener('click', toggleNav.bind(menu, event, 'active'), false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment