javascript - jQuery combining 2 bind functions (TypeError: a is undefined) -


please give me piece of advice.

i'm trying write function 2 similar functions combining common parts.
these 2 functions, i'd "selected1" & "selected2", , use these value in next function, calc.

var selected; var selected1; var selected2;  $( '.type1' ).bind('change', function () {     var values = [];     $( 'input.type1:checkbox:checked' ).each(function ( _, el ) {         values.push( base64decode( $( el ).data( 'val' ) ) );         if ( values.length > 1 ) {             values[0] = $.map(values[0], function (val, i) {                 return values[0][i] | values[1][i];             });             values.splice(1,1);         }     });     selected1 = result;     calc(); });  $( '.type2' ).bind('change', function () {     var values = [];     $( 'input.type2:checkbox:checked' ).each(function ( _, el ) {         values.push( base64decode( $( el ).data( 'val' ) ) );         if ( values.length > 1 ) {             values[0] = $.map(values[0], function (val, i) {                 return values[0][i] | values[1][i];             });             values.splice(1,1);         }     });     selected2 = result;     calc(); });  function calc () {     var selected3 = $.map(selected1, function (val, i) {         return selected1[i] & selected2[i];     });     selected = base64encode( selected3 );     overlays(); }; 

when write above, both selected1&2 defined global variables, function calc works.

however, doesn't work on rewrite code. firebug says "typeerror: undefined". here code:

function tab (checkedtab, selected) {     var values = [];     checkedtab.each(function ( _, el ) {         values.push( base64decode ($( el ).data( 'val' ) ) );         if ( values.length > 1 ) {             values[0] = $.map(values[0], function (val, i) {                 return values[0][i] | values[1][i];             });             values.splice(1,1);         }     });     selected = values[0]; }; $( '.type1' ).bind('change', function () {     tab ($( 'input.type1:checkbox:checked' ), 'selected1');     calc(); }); $( '.type2' ).bind('change', function () {     tab ($( 'input.type2:checkbox:checked' ), 'selected2');     calc(); }); 

could please tell me why code doesn't work?

thank comments, peter.
after debugging variables, works!
may not best way, post rewrite code reference.

var values;  function tab (checkedtab) {     values = [];         checkedtab.each(function ( _, el ) {         values.push( base64decode ($( el ).data( 'val' ) ) );         if ( values.length > 1 ) {             values[0] = $.map(values[0], function (val, i) {                 return values[0][i] | values[1][i];             });             values.splice(1,1);         }     });     return values[0]; }; $( '.type1' ).bind('change', function () {     selected1 = tab ($( 'input.type1:checkbox:checked' ));     calc(); }); $( '.type2' ).bind('change', function () {     selected2 = tab ($( 'input.type2:checkbox:checked' ));     calc(); }); 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -