Are you saying that the markup (as it downloads form the server (including your JS above)) is not validating? Or your markup AFTER the JS has made its changes is not validating? I'm guessing its the former. If it is, try putting all your JS in an html comment tag. like this:
<!-- $(this)... -->

