Should this question have been migrated from StackOverflow to CodeReview? OP was asking a fairly specific question about how to do something with jQuery; he wasn't simply asking for feedback on his code.
I believe so, as I migrated it over.
The key indicators are in the line that random points out (emphasis mine):
So the poster has a working solution and is looking to refactor it.
In my mind, that makes it a much better fit for Code Review.