In this sample, the watermark plug-in doesn’t work at first, because the div “tbFirstName” is not loaded yet.
$(document).ready(function () {
$('#tbFirstName').watermark('Required information');
});
The (.live) Jquery tool or (.on) tool seem to work based on a event such as “click” or “blur”. Do you know how I can get this watermark effect on a div that loads in the future, and without having to assign to a user-event?
Many thanks!
You could try the
DOMNodeInsertedevent:JS Fiddle proof-of-concept.
Note that it’s important to attach the
on()method to whatever element is closest the closest parent to the to-be-attached$('#tbFirstName')element, otherwise it will run every time something’s appended to the DOM.It’s also worth noting, just for completion, that it’d be far easier, and more efficient, to bind the
watermark()while creating or appending that element to the DOM.References:
DOMNodeInsertedat W3.org.DOMNodeInsertedat MDN.