Created
June 5, 2014 16:00
-
-
Save grayghostvisuals/55c225c9a4c6a9fd7d67 to your computer and use it in GitHub Desktop.
Passing Arguments to Event Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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