<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Sayed Ibrahim Hashimi - MSBuild, Web Deploy (MSDeploy), ASP.NET - jQuery-plugin</title>
    <link>http://sedodream.com/</link>
    <description>MSBuild, C#, Visual Studio and more</description>
    <language>en-us</language>
    <copyright>Sayed Ibrahim Hashimi</copyright>
    <lastBuildDate>Wed, 31 Mar 2010 04:24:07 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>sayed.hashimi@gmail.com</managingEditor>
    <webMaster>sayed.hashimi@gmail.com</webMaster>
    <item>
      <trackback:ping>http://sedodream.com/Trackback.aspx?guid=539af450-2b61-46e1-a6de-8a62cbb5b4f9</trackback:ping>
      <pingback:server>http://sedodream.com/pingback.aspx</pingback:server>
      <pingback:target>http://sedodream.com/PermaLink,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</pingback:target>
      <dc:creator>Ibrahim</dc:creator>
      <wfw:comment>http://sedodream.com/CommentView,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</wfw:comment>
      <wfw:commentRss>http://sedodream.com/SyndicationService.asmx/GetEntryCommentsRss?guid=539af450-2b61-46e1-a6de-8a62cbb5b4f9</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">Have you seen those <a href="http://sedotech.com/LiveDemo/TextWithHints">text
boxes</a> on the web that have a hint contained inside of it and wondered how you
could implement that? It's pretty easy with <a href="http://jquery.com/">jQuery</a>,
and there already exist some plugins that you can use, for example here is <a href="http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/">one</a>.
When I set out to do this I didn’t even look to see what was out there because I wanted
to write a <a href="http://plugins.jquery.com/">jQuery plugin</a>, but the solution
that I can up with is not that different from that one. 
<p>
Here is how I wanted the plugin to behave
</p><ol><li>
Add a specified hint to the text box, if the input element was not focused and empty 
</li><li>
When the text box was focused, the hint should disappear 
</li><li>
When a form is submitted all hints should be removed prior to ensure that they are
not incorrectly submitted 
</li></ol><p>
First what I did was to create the a file named <em>jquery.sedotech.inputWithHint.js</em>.
You should name your plugins using this naming convention
</p><p><em>jquery.<strong>customString</strong>.<strong>pluginName</strong>.js</em></p><p>
Here is the source for the plugin
</p><pre class="brush: js;">(function ($) {
    var hintClassName = 'inputWithTextHint';

    $.fn.addHint = function (hint) {
        var filteredSet = this.filter('input:text, textarea');
        filteredSet.each(function () {
            // In here 'this' refers to an individual element
            doAddHint($(this), hint);
        });

        // Find all forms and update the pre post to remove the hint
        $('form input:submit').click(function () {
            $('input:text, textarea').each(function () {
                if ($(this).hasClass(hintClassName) &amp;&amp; $(this).attr('hint')
                &amp;&amp; $(this).val() == $(this).attr('hint')) {
                    $(this).val('');
                }
            });
        });
    }

    function doAddHint(target, hint) {
        // Only add hint if the target is empty
        if (target.val() == '') {
            addHintToInput(target, hint);

            target.focus(function () {
                // If the target has the hint class on it then a hint must be showing
                //  when hint is showing put cursor at the begining
                if ($(this).hasClass(hintClassName)) {
                    // remove the hint
                    $(this).val('');
                    // remove class
                    $(this).removeClass(hintClassName);
                }
            });

            target.blur(function () {
                // If no text then add hint class back
                if ($(this).val() == '') {
                    addHintToInput(target, hint);
                }
            });
        }
    }

    function addHintToInput(target, hint) {
        target.val(hint);
        target.addClass(hintClassName);
        // add attribute to the target to store hint
        target.attr('hint', hint);
    }
})(jQuery);</pre>
Some things to take note of. When you are creating a plugin in you should use the
pattern <pre class="brush: js;">(function ($) {
    // plugin code here
})(jQuery);</pre><p>
Take note of the <strong>($)</strong> as the parameter and <strong>(jQuery)</strong> at
the end. What is happening here is that you are defining an anonymous function declaring
a parameter named $ and then invoking that function passing in the <strong>jQuery</strong> object.
You do this because when you are authoring plugins the $ variable is not available,
you have to use the other alias jQuery, but that’s just way too difficult. If you
use the pattern you are ensured that the <strong>$</strong> alias is available and
it won’t conflict with other Javascript libraries.
</p><p>
Beyond this all I’m doing is filtering the list of items which are passed in, with
the expression <strong><em>var filteredSet = this.filter('input:text, textarea')</em></strong>,
to make sure that the plugins doesn’t touch elements which it is not familiar with
modifying. After I add the hint, by calling <strong><em>doAddHint</em></strong> on
each element in the filtered set, I make sure that the forms on the page are not submitted
with those hints.
</p><h3>Resource Links
</h3><ul><li><a href="http://sedotech.com/Resources#jQuery">All my jQuery resources</a></li><li><a href="http://sedotech.com/LiveDemo/TextWithHints">Live Demo for this plugin</a></li></ul><p>
Sayed Ibrahim Hashimi
</p></body>
      <title>jQuery: Creating a plugin to place hints in text boxes</title>
      <guid isPermaLink="false">http://sedodream.com/PermaLink,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</guid>
      <link>http://sedodream.com/2010/03/31/jQueryCreatingAPluginToPlaceHintsInTextBoxes.aspx</link>
      <pubDate>Wed, 31 Mar 2010 04:24:07 GMT</pubDate>
      <description>Have you seen those &lt;a href="http://sedotech.com/LiveDemo/TextWithHints"&gt;text boxes&lt;/a&gt; on
the web that have a hint contained inside of it and wondered how you could implement
that? It's pretty easy with &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;, and there already
exist some plugins that you can use, for example here is &lt;a href="http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/"&gt;one&lt;/a&gt;.
When I set out to do this I didn’t even look to see what was out there because I wanted
to write a &lt;a href="http://plugins.jquery.com/"&gt;jQuery plugin&lt;/a&gt;, but the solution
that I can up with is not that different from that one. 
&lt;p&gt;
Here is how I wanted the plugin to behave
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Add a specified hint to the text box, if the input element was not focused and empty 
&lt;/li&gt;
&lt;li&gt;
When the text box was focused, the hint should disappear 
&lt;/li&gt;
&lt;li&gt;
When a form is submitted all hints should be removed prior to ensure that they are
not incorrectly submitted 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
First what I did was to create the a file named &lt;em&gt;jquery.sedotech.inputWithHint.js&lt;/em&gt;.
You should name your plugins using this naming convention
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;jquery.&lt;strong&gt;customString&lt;/strong&gt;.&lt;strong&gt;pluginName&lt;/strong&gt;.js&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
Here is the source for the plugin
&lt;/p&gt;
&lt;pre class="brush: js;"&gt;(function ($) {
    var hintClassName = 'inputWithTextHint';

    $.fn.addHint = function (hint) {
        var filteredSet = this.filter('input:text, textarea');
        filteredSet.each(function () {
            // In here 'this' refers to an individual element
            doAddHint($(this), hint);
        });

        // Find all forms and update the pre post to remove the hint
        $('form input:submit').click(function () {
            $('input:text, textarea').each(function () {
                if ($(this).hasClass(hintClassName) &amp;amp;&amp;amp; $(this).attr('hint')
                &amp;amp;&amp;amp; $(this).val() == $(this).attr('hint')) {
                    $(this).val('');
                }
            });
        });
    }

    function doAddHint(target, hint) {
        // Only add hint if the target is empty
        if (target.val() == '') {
            addHintToInput(target, hint);

            target.focus(function () {
                // If the target has the hint class on it then a hint must be showing
                //  when hint is showing put cursor at the begining
                if ($(this).hasClass(hintClassName)) {
                    // remove the hint
                    $(this).val('');
                    // remove class
                    $(this).removeClass(hintClassName);
                }
            });

            target.blur(function () {
                // If no text then add hint class back
                if ($(this).val() == '') {
                    addHintToInput(target, hint);
                }
            });
        }
    }

    function addHintToInput(target, hint) {
        target.val(hint);
        target.addClass(hintClassName);
        // add attribute to the target to store hint
        target.attr('hint', hint);
    }
})(jQuery);&lt;/pre&gt;
Some things to take note of. When you are creating a plugin in you should use the
pattern &lt;pre class="brush: js;"&gt;(function ($) {
    // plugin code here
})(jQuery);&lt;/pre&gt;
&lt;p&gt;
Take note of the &lt;strong&gt;($)&lt;/strong&gt; as the parameter and &lt;strong&gt;(jQuery)&lt;/strong&gt; at
the end. What is happening here is that you are defining an anonymous function declaring
a parameter named $ and then invoking that function passing in the &lt;strong&gt;jQuery&lt;/strong&gt; object.
You do this because when you are authoring plugins the $ variable is not available,
you have to use the other alias jQuery, but that’s just way too difficult. If you
use the pattern you are ensured that the &lt;strong&gt;$&lt;/strong&gt; alias is available and
it won’t conflict with other Javascript libraries.
&lt;/p&gt;
&lt;p&gt;
Beyond this all I’m doing is filtering the list of items which are passed in, with
the expression &lt;strong&gt;&lt;em&gt;var filteredSet = this.filter('input:text, textarea')&lt;/em&gt;&lt;/strong&gt;,
to make sure that the plugins doesn’t touch elements which it is not familiar with
modifying. After I add the hint, by calling &lt;strong&gt;&lt;em&gt;doAddHint&lt;/em&gt;&lt;/strong&gt; on
each element in the filtered set, I make sure that the forms on the page are not submitted
with those hints.
&lt;/p&gt;
&lt;h3&gt;Resource Links
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://sedotech.com/Resources#jQuery"&gt;All my jQuery resources&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://sedotech.com/LiveDemo/TextWithHints"&gt;Live Demo for this plugin&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Sayed Ibrahim Hashimi
&lt;/p&gt;</description>
      <comments>http://sedodream.com/CommentView,guid,539af450-2b61-46e1-a6de-8a62cbb5b4f9.aspx</comments>
      <category>Javascript</category>
      <category>jQuery</category>
      <category>jQuery-plugin</category>
    </item>
  </channel>
</rss>