Hopefully this example will illustrate my point better than simply trying to explain it:
I'm using one of the many jQuery watermark plugins. To attach a watermark to a textbox, the syntax is:
$(document).ready(function(){
$("#item_description").watermark("Description");
});
This works wonderfully if I have a page that already contains a textbox with id = item_description. However, if I try to do any ajaxy-type stuff by loading up a partial view containing that textbox on a button click, then $(document).ready()
has already been called and the watermark is not applied. I tried putting a seperate $(document).ready()
inside of the partial view, but it looks like rails (?) strips out any <script>
blocks when it renders the page, meaning that it still never gets called.
Is there an easy way to do this? Am I just missing something obvious?
Edit: I'm using the jQuery Colorbox plugin to load up the partial view inside of a lightbox (via $(.colorbox).colorbox();
) -- I'm not actually doing the ajax request myself. If I'm understanding the answers so far, the best way might be to use the callback functionality of the plugin to execute any javascript I need inside of the partial.
Working Example: Based on the feedback, I was able to get this working using colorbox by simply changing the colorbox call to be:
$(".colorbox").colorbox({}, function(){
$(".description").watermark("Description"); //Changed to a class
});