0

I have a similar question to this Stack post; but, would like to call the function in a jquery chain. Here is my pseudo code:

function togglePanels(f) {
  var customchain;
  if (f == 'n') {
    customchain = $.next();
  } else {
    customchain = $.prev();
  }
  let $next_or_prev_panel = $current_panel
    .parents("div")
    .eq("2")
    .customchain("div")
    .find(".panel");
}

I imagine the solution will involve prototyping the jQuery library somehow?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Jim22150
  • 511
  • 2
  • 8
  • 22
  • 1
    Well, that code wouldn't work, but you could just do `customchain = f == 'n' ? "next" : "prev"; .... .eq("2")[customchain]("div").find(".panel");`. – Heretic Monkey Apr 23 '20 at 16:26

1 Answers1

1

One solution could be to use conditional (ternary) operator with a string to set customchain like:

customchain = f == 'n' ? 'next' : 'prev';

and then use it dynamically like:

let $next_or_prev_panel = $current_panel
    .parents("div")
    .eq("2")[customchain]("div")
    .find(".panel");
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Thanks, you understood what I am actually trying to do.. no prototyping needed. But out of curiosity, can you point to an example of prototyping a new function within a jQuery object and calling it within a chain? – Jim22150 Apr 23 '20 at 16:42
  • You can look into [How to call a custom function as part of a jQuery function chain](https://stackoverflow.com/questions/42565943/how-to-call-a-custom-function-as-part-of-a-jquery-function-chain) – palaѕн Apr 23 '20 at 17:05