I'm attempting to call a function that already works with event listeners.
For example I set my event listener:
$('#country_choice').on('change', {opt: 1}, revealOption);
and the revealOption function contains a switch statement:
function revealOption(event){
var n = event.data.opt;
switch(n) {
case 1:
// 1 reveal city
$('#city_form').removeClass('hidden');
//alert( "Reveal city" );
break;
case 2:
// 2 reveal produce
$('#prod_form').removeClass('hidden');
//alert( "Reveal prod" );
break;
case 3:
// 3 reveal all
$('#city_form').removeClass('hidden');
$('#prod_form').removeClass('hidden');
alert( "Reveal all" );
break;
}
}
However, I'm trying to be able to use this function within other functions that aren't part of the event handler.
function dev(){
revealOption(3);
}
I'd prefer to avoid writing another function to do the same task, but finding it difficult to find out whether it's possible.
The closest question I've found while researching on SO is function handle jQuery event and parameter?, however this deals with passing the event object through to another function, not passing a value.
Any help would be gratefully appreciated :)
function revealOption(opt). Providing the function reference to an event handler is some nice syntactic sugar that JS offers, but you can quickly tie yourself in knots when you need to change the scope of that function.