3

Possible Duplicate:
Apply jquery function to ajax content

I read the Article about binding fancybox to a ajax loaded content via .on

Problem is: I need the plugin to be activated upon every reload of the inner DIV.

Here's my HTML:

<div id="page">
 <div id="ajax_container">
  content with selectboxes
 </div>
</div>

Here's my JS

jQuery("#page").on("focus", function(){
jQuery("select").selectbox({
        effect: "fade"
 }); // selectbox
}); // on

The content now needs mouse movement to convert the selectboxes. I haven't found any other working event. Moreover, it takes quite long for the SELECTs to be converted.

How can every loaded AJAX content just show up with the plugins applied?

note: the AJAX request is made by another js-framework/ruby

Community
  • 1
  • 1
cukabeka
  • 530
  • 2
  • 9
  • 22

1 Answers1

1

You can bind the selectbox inside the success callback of the ajax request like this

$.post('URL',{data:data},function(data){
                $(data).appendTo('#ajax_container').filter('select').selectbox({
                   effect: "fade"
                });
});

Working Fiddle

As you cant initialize the plugin in that ajax success callback, you can register a global success callback and that may work. Check this

$('body').ajaxSuccess(function(){
   $('select').selectbox({
                       effect: "fade"
                    });
});

More on .ajaxSuccess

Prasenjit Kumar Nag
  • 13,391
  • 3
  • 45
  • 57
  • thanks! unfortunately I can't influence the ajax request. the ajax part is done by ruby on rails. – cukabeka Jun 13 '12 at 14:07
  • thank you for that update! looks promising, but now the complete ajax requests seem to be broken. if I activate the ajaxSuccess via the JS console, ajax requests don't work at all anymore; to try it, I went with the following code: jQuery('#page').ajaxComplete(function(){ alert('ajax'); }); – cukabeka Jun 13 '12 at 15:28
  • Just wanted to comment on the first one: Since jquery-ujs, there are callbacks like `ajax:before` you can bind to. – pdu Sep 14 '12 at 15:20