I tried loads of variations on this idea and ended up using something along these lines. Thought I'd share it.
Then I used noscript tags to enter an alternative for non-JS users, i.e:
I tested it in IE8 and it's all cool for me and it also validates.
Hope this might help someone out there!