jquery - Assign each index argument different function -


i have code.

$('#my-container-div').on('onalbumload', function(event, index) { ... }); 

i need assign different function each instance of index , @ loss.

in non-coder terms, i'm looking for: if index equals 0 this, if index equals 1 this, if index equals 3 this, , on.

i don't follow overall context of you're trying enough know if best option or not, can use code @ index , decide next.

that can done if/else or switch statement or function table.

// if/else $('#my-container-div').on('onalbumload', function(event, index) {    if (index === 0) {        func1();    } else if (index === 1) {        func2();    } });  // switch $('#my-container-div').on('onalbumload', function(event, index) {      switch(index) {          case 0:              func1();              break;          case 1:              func3();              break;      } });   // function table var ftable = [func1, func2, func3, func4];  $('#my-container-div').on('onalbumload', function(event, index) {     if (index < ftable.length) {         ftable[index]();     } }); 

any of these work.

if number of indexes compare 2 or less, use if/else.

if number of indexes more 2 , you're checking contiguous set of indexes, i'd use function table because it's more extensible without write new code (you add new function array).

if indexes checking more 2 , not contiguous, i'd use switch statement.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -