qualcentric-web-solutions / jquery-watermark

Automatically exported from code.google.com/p/jquery-watermark
0 stars 0 forks source link

A dynamic function should be called to retreive the watermark text per element basis. #35

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I would like to be able to specify a function in the options, which is 
dynamically invoked on each element selected.

My suggestion is to add a property on the options-structure, i.e. 
"watermarkTextCallback".
And if a function is specified, call that method for each element (parameters 
should be the currently processed input-element, the options and the default 
text). If no function is specified the default text is used.

This would enable me to i.e. read the label from each input-element and have 
that as my watermark-text. I like to keep this in the markup instead of 
specifying it in javascript.

I have attached a working version.
In the attached file look for: watermarkTextCallback
I've also moved the text- and the hasText-variables into the each-method.

Example:

var options = { 
   watermarkTextCallback: function (input, options, defaultText) { return input.attr("title"); }
};

$("#myLoginForm input").watermark("My default text", options);

Kind regards
Richard Bladh

Original issue reported on code.google.com by richard....@gmail.com on 6 Oct 2010 at 3:42

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks for the submission Richard.  I'll look into adding this capability.

Based on jQuery conventions, I would probably add such a feature by specifying 
an alternate calling signature, rather than adding a new option.

So the plugin would look for either a string or a function in the first 
argument, and change its behavior based on what it sees there.

You can liken it to the .val() jQuery function, which works similarly, either 
evaluating a string or function.

Besides, if specifying a function, I can't see the need for a default watermark 
string, because it would simply obtain the initial value from the function.

Original comment by t...@speednet.biz on 3 Dec 2010 at 4:10

GoogleCodeExporter commented 8 years ago

Original comment by t...@speednet.biz on 3 Dec 2010 at 4:14

GoogleCodeExporter commented 8 years ago
Yes I agree!
Looking forward to see our version of this so I can remove my "hack" on your 
solution.

Kind regards
Richard

Original comment by richard....@gmail.com on 7 Dec 2010 at 12:41

GoogleCodeExporter commented 8 years ago
Sorry Richard, I looked into adding this feature and it becomes overly-complex 
in the implementation.

The logic works great, up until the point where you have to evaluate if a value 
in the input element is a watermark or not.  With a dynamically-changing value, 
you have to start saving old watermark values and other complex things, and it 
just adds too much bulk and overhead for minimal gain.

If you have a dynamic watermark, you can still make it happen by dynamically 
redefining the value.  See the included demo page for an example of this.

The feature did show promise, but for the reasons given, I'm going to have to 
take it off the list.

Original comment by t...@speednet.biz on 21 Dec 2010 at 6:24