0

I have this code.

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

I need to assign a different function to each instance of index and am at a loss.

In non-coder terms, I'm looking for: If index equals 0 do this, if index equals 1 do this, if index equals 3 do this, and so on.

NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
macmike78
  • 5
  • 2

2 Answers2

0

I don't follow the overall context of what you're trying to do well enough to know if this is the best option or not, but you can just use code to look at the index and then decide what to do next.

That can be done with if/else or with a switch statement or with a 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 would work.

If the number of indexes to compare is two or less, I would use the if/else.

If the number of indexes is more than two and you're checking for a contiguous set of indexes, I'd use the function table because it's more easily extensible without write new code (you just add a new function to the array).

If the indexes you are checking for are more than two and not contiguous, I'd probably use the switch statement.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • That's exactly what I was looking for! Simple as that. Used option 1 & it worked like a charm. Now time for some Javascript/jQuery lessons. Geesh! Thanks so much for the prompt help jfriend00!! – macmike78 Aug 31 '15 at 02:45
  • @macmike78 - since it looks like you might be new here, did you know that if you get your question answered, then you can indicate that to the community by selecting the green checkmark next to the answer that helped you the most. This will earn both you and the person who provided the answer some reputation points will (after you earn some more) will get your more privileges on the site. – jfriend00 Aug 31 '15 at 03:09
  • Well, do you have me pegged! ;) Done & thanks again. – macmike78 Aug 31 '15 at 03:15
  • @guest271314 - just educating the newbie OP on how the process works here. They will hopefully be a more informed participant now. – jfriend00 Aug 31 '15 at 03:23
0

Try creating array of functions corresponding , with each function corresponding to an index ; utilize .trigger(event, [args]) to call "onAlbumLoad" event with index ; e.g., 0 as parameter

var fns = [
  function fn0() {
    $(this).html("a")
  },
  function fn1() {
    $(this).html("b")
  },
  function fn2() {
    $(this).html("c")
  }
];

var container = $("#my-container-div");

container.on("onAlbumLoad", function(event, index) {
  fns[index].call(this)
});

$("input").on("change", function() {
  container.trigger("onAlbumLoad", [this.value])
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="my-container-div">container</div>

<input type="checkbox" value="0" />
<input type="checkbox" value="1" />
<input type="checkbox" value="2" />
guest271314
  • 1
  • 15
  • 104
  • 177