+2  A: 

Put your jQuery code into $(document).ready(function () {...your code...}). This will make it executed after browser creates DOM tree for your page. Otherwise javascript is not able to search/manipulate DOM elements correctly. It will look as following:

$(document).ready(function () {
    $('.testbutton').click(function() { 
          $('#test').val("haha");
    });
});

Update:

If your HTML code is loaded dynamically, then use live to bind event handler:

$(document).ready(function () {
    $('.testbutton').live("click", function() { 
          $('#test').val("haha");
    });
});
Ivan Nevostruev
Well if it's a dialog, it's possible that it's being loaded via AJAX, and that it's an HTML fragment. If that's the case, then there'll be no "ready" event fired for that content.
Pointy
It should work...
Rifat
Nope, doesn't work. The dialog is a post DOM event anyways, so adding a document.ready inside a dialog doesn't quite make sense, I think?
Rio
you can bind it with click event in ajax return function...
Rifat
Look at updated version
Ivan Nevostruev
The updated version didn't work for me. Does it matter where it's put?
Rio
A: 

I guess you should wrap your code inside of a $(function() { //code here }); to make sure your code is run only when your DOM is ready.

Ricardo Rodrigues
Again, if the dialog content is an HTML fragment loaded via AJAX, there's no "ready" event. Of course, I don't know that that's how his dialog works, because the original question doesn't describe the environment very well.
Pointy
In case you're loading the HTML via AJAX, you should run the bind method after loading the HTML to the DOM, it depends on the specific use case.
Ricardo Rodrigues
A: 

The problem ended up being the # in test. For some reason the replacement works with a class identifier instead of a id identifier. I suppose it's because the # will replace only one instance, and for some reason that I have yet to discover, there is more than one instance of the dialog (or a hidden one).

Thanks for all your suggestions!

Rio