0

I have a function called foo which is supposed to return the current visible element, but returns undefined instead.

function foo(){
  $('.bar').on('shown', function(){           // 'shown' is a bootstrap .js event
    return ($(this).attr('id').split('baz')); // returns 'undefined'
  });
}

According to Pointy's answer here, it does not make sense to return values from event handlers. If that's the case, what would be the conventional way to retrieve the return of foo(); when needed?

8
  • You could cache out the data you need in a higher scoped var (before foo). Commented Jun 16, 2016 at 3:56
  • Show html of .bar Commented Jun 16, 2016 at 3:57
  • .bar is actually .accordion-toggle (Bootstrap accordion class). If I CL it as $('.accordion-toggle').length, I get 3, which matches the number of accordions I have on page. Commented Jun 16, 2016 at 3:59
  • @JamesCollier to a global var? Commented Jun 16, 2016 at 4:00
  • @Dimitry_N not global because that's frowned upon. Say foo() is in a class, make a var at the class level. Commented Jun 16, 2016 at 4:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.