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?

1
  • 1
    Well, that code wouldn't work, but you could just do customchain = f == 'n' ? "next" : "prev"; .... .eq("2")[customchain]("div").find(".panel");. Commented Apr 23, 2020 at 16:26

1 Answer 1

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");
Sign up to request clarification or add additional context in comments.

2 Comments

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.