A better way to implement custom jQuery code?

Discourse is an SPA, a single-page application. This also means the javascripts you add in <head> only execute once on the initial page load unless you use the correct methods. You may need to change your code to fire based upon an event handler / listener.

HTH