We study the production and polarization of J/ψ mesons within a jet in proton-proton collisions at the LHC. We define the J/ψ-jet fragmentation function as a ratio of differential jet cross sections with and without the reconstructed J/ψ in the jet. We demonstrate that this is a very useful observable to help explore the J/ψ production mechanism, and to differentiate between different nonrelativistic QCD global fits based on inclusive J/ψ cross sections. Furthermore, we propose to measure the polarization of J/ψ mesons inside the jet, which can provide even more stringent constraints for the heavy quarkonium production mechanism.