tags:

views:

44

answers:

2

I have the following function which applies watermark text to any text box on the page with the id "Search":

jQuery(document).ready(function ($) {

    $("#Search").watermark({
        watermarkClass: 'watermarkOn',
        defaultText: $("#Search").attr("watermarkText")
    });
});

The problem I'm having is that that this doesn't get applied when I asynchronously load a panel that contains the text box, because it occurs in the jQuery(document).ready function, which has already fired.

Is there anything I can do to make sure that any asynchronously loaded text boxes have this function applied to them? Thanks.

A: 

look up the .live() method to bind events, to elements created previously, or in the future, as in your case, asynchronously.

Ref: http://api.jquery.com/live/

It may be possible to alter the watermark plugin / method you have to use .live()

.live() will only work for events and it won't allow you to attach a plugin to elements that will be created in the future.

For that you can look at liveQuery a plugin that will allow you to attach plugins to elements that may be created in the future.

eg.

//initiate the watermark plugin on all elements with class 'search' existing now or created in the future.
    $('.Search').livequery(function() {
        $(this).watermark({
            watermarkClass: 'watermarkOn',
            defaultText: $("#Search").attr("watermarkText")
        });
    });
Moin Zaman
`.live()` does not work with plugins, it's event based.
Nick Craver
@Moin - even with your edit, this isn't how things work...I don't mean this to sound rude though it will, *no* answer is better than a wrong one, and you've posted many of these the past few days.
Nick Craver
@Nick - no offence taken mate. I've learnt more as a result of posting 'wrong' answers. Could you point out what you make out to be 'many' of them?
Moin Zaman
Also I know `.live()` doesn't work with plugins, its event based. Events which a plugin may use, therefore being able to leverage off `.live()`
Moin Zaman
@Moin - You simply can't use `.live()` for this...when a new element is added things need to be done to it, namely adding the watermark class and probably some sort of `focus`/`blur` behavior...`.live()` can't do this, it has no knowledge of added elements, so it has no mechanism to recognize elements added to the DOM and add classes/properties to them. This is true of *most* plugins, otherwise it wouldn't be a plugin, it'd be a simple `.live()` handler.
Nick Craver
+2  A: 

If you're using $(selector).load(), then call the .watermark() in the callback.

$('someSelector').load('/some/path', function( response ) {
    $('someNewElement').find('input').watermark({
        watermarkClass: 'watermarkOn',
        defaultText: $("#Search").attr("watermarkText")
    });
});

If the options are all the same, you could store them in a variable to be reused.

var options = {
           watermarkClass: 'watermarkOn',
           defaultText: $("#Search").attr("watermarkText")
          };

$("#Search").watermark( options );

$('someSelector').load('/some/path', function( response ) {
    $('someNewElement').find('input').watermark( options );
});
patrick dw
Find some way of calling it after you asynchronously load the content. Even if its not load(), however you're doing it will have a way of executing code on the successful completion of the script.
Thomas Clayson