Do you have the above JS wrapped such that it waits to execute until after the document has finished loaded? If you don't, what may be happening is that the javascript is trying to act on elements that have not yet been rendered. So, perhaps #loginform button
does not yet exist when it is attempting to bind to that element.
Wrap your JS in this:
jQuery(function(){
// All JS that executes on load here.
});