I am trying to be a good boy and use Drupal Behaviors to manage my JavaScript. My understanding is that one of the advantages of using Behaviors is that it frees you from having to use .live() to bind event handlers to dynamically created elements. However, I find that I have to use .live() all the time. What am I doing wrong?
Here is a simple example of what I'm talking about:
Drupal.behaviors.MySearch = {
attach: function (context, settings) {
var searchBox = $('#my-search-box');
searchBox.append("<input type='text'></input>");
searchBox.append("<button>Search</button>");
$('#my-search-box button').click(function(){
console.log("This does not work.");
});
$('#my-search-box button').live('click', function(){
console.log("This does work.");
});
};