This had been my previous attempt from a while back. Definitely not elegant by any means. Your javascript solution makes way more sense.