I was trying to get this same feature and came up with this, similar to Giorgio's approach:

Code:
?>
<script>
    $(document).ready(function()
...