Skip to content

Really Simple “Single Call” Trick for jQuery Watermark Plugin

If you guys haven’t seen this terrific jQuery Watermark Plugin go check it out over here.

I just installed it today and found a nice way to watermark all my input boxes by a neat jQuery .each() trick. If you’ve used this plugin but got a bit annoyed having to invoke the watermark and set the contents in JavaScript for each one, give this a shot.

JavaScript:

// Scan and set a watermark for every matching input element and set with 'watermark' flag
$(document).ready(function() {
    $('input').each(function() {
        $(this).watermark($(this).attr('watermark'));
    });
});

Html:

<input type="text" id="name" watermark="Full Name" />
<input type="text" id="city" watermark="City or Suburb" />

The JS will iterate through all input controls on your page (the above example being two), and set the watermark text to that which you have specified in a custom attribute named “watermark”. Pretty easy and cuts down code.

Great plugin, cheers Todd.

VN:F [1.9.1_1087]
Rating: 4.0/5 (7 votes cast)
VN:F [1.9.1_1087]
Rating: -1 (from 3 votes)
Really Simple "Single Call" Trick for jQuery Watermark Plugin, 4.0 out of 5 based on 7 ratings
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

3 Comments

  1. daddywoodland

    Neat trick but I think you’d be better setting the watermark as the ‘title’ attribute and reading from that rather than adding a new attribute which will invalidate your html.

    VA:F [1.9.1_1087]
    Rating: 2.0/5 (1 vote cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 17-Dec-09 at 1:00 am | Permalink
  2. Graham O'Neale

    Good point. Although you have to deal with a tooltip on every textbox if you are running FF and/or Chrome? But better than invalid html I suppose.

    VN:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 17-Dec-09 at 9:22 am | Permalink
  3. Hey Graham, thanks for the props, glad you’re enjoying the plugin. I do check the issue list somewhat regularly, so feel free to leave anything you like over there.

    VA:F [1.9.1_1087]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.1_1087]
    Rating: 0 (from 0 votes)
    Posted on 09-Jan-10 at 6:46 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...