Skip to content

Commit

Permalink
jQuery 1.6 compatibility and namespace all events.
Browse files Browse the repository at this point in the history
  • Loading branch information
mudge committed Sep 29, 2011
1 parent ba24580 commit b8c1bdb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 64 deletions.
4 changes: 2 additions & 2 deletions BSD-LICENSE.txt
@@ -1,4 +1,4 @@
Copyright (c) 2008-2009, Paul Mucur, http://mucur.name
Copyright (c) 2008-2011, Paul Mucur, http://mudge.name

All rights reserved.

Expand All @@ -12,7 +12,7 @@ modification, are permitted provided that the following conditions are met:
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of http://mucur.name nor the names of its contributors may
* Neither the name of http://mudge.name nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.

Expand Down
28 changes: 19 additions & 9 deletions README.markdown
@@ -1,6 +1,4 @@
*WARNING: The current version of this plugin is not yet compatible with jQuery 1.6+*

jQuery Form Example Plugin 1.4.3
jQuery Form Example Plugin 1.5
======================================

This is a jQuery plugin to populate form inputs with example text that
Expand Down Expand Up @@ -68,6 +66,20 @@ Please note that metadata will be overridden by arguments, e.g.

The example will be set to "Another example" instead of "An example".

All events are namespaced with `.example` so they can be selectively unbound with
`unbind('.example')`. The full list is as follows:

* `unload.example` on the `window`;
* `submit.example` on any affected `form`s;
* `focus.example` on affected inputs;
* `change.example` on affected inputs;
* `blur.example` on affected inputs.

Downloading
-----------

[Download jQuery Example 1.5](https://github.com/mudge/jquery_example/zipball/v1.5).

Testing
-------

Expand All @@ -77,11 +89,9 @@ in the test/ directory and run by opening index.html in your browser.
Compatibility
-------------

This plugin has been tested with jQuery 1.1 to 1.5 and should work in all
browsers supported by jQuery itself (it has been tested with Safari 4, Google Chrome 0.2.149.29, Mozilla Firefox 3, Opera 9.52 and
Internet Explorer 7).

It is not yet compatible with jQuery 1.6's changes to `val()`.
This plugin has been tested with jQuery 1.1 to 1.6 and should work in all
browsers supported by jQuery itself (it has been tested with Safari 4,
Google Chrome 0.2.149.29, Mozilla Firefox 3, Opera 9.52 and Internet Explorer 7).

Author
------
Expand All @@ -102,7 +112,7 @@ The code to support the Metadata plugin was contributed by DeLynn Berry (http://
Licensing
---------

Copyright (c) Paul Mucur (http://mucur.name), 2007-2008.
Copyright (c) Paul Mucur (http://mudge.name), 2007-2011.

Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
Licenses.
Expand Down
9 changes: 3 additions & 6 deletions demo.html
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<h1>jQuery Form Example Plugin</h1>
<p>This is a usage guide for the <a href="http://mucur.name/posts/jquery-example">jQuery Form Example plugin</a> by <a href="http://mucur.name/">Paul Mucur</a>. Below you will find several different use cases including the relevant HTML and JavaScript source code.</p>
<p>This is a usage guide for the <a href="https://github.com/mudge/jquery_example">jQuery Form Example plugin</a> by <a href="http://mudge.name/">Paul Mucur</a>. Below you will find several different use cases including the relevant HTML and JavaScript source code.</p>
<p>As of version 1.4, the plugin now comes with a <a href="http://docs.jquery.com/QUnit">QUnit</a> test suite.</p>
<form action="javascript:alert('Form submitted, all examples should be cleared.');">
<div class="case">
Expand Down Expand Up @@ -183,10 +183,7 @@ <h3>JavaScript</h3>
</div>
</form>

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.2");
</script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="http://dev.jquery.com/~john/plugins/profile/jquery-profile.js"></script>
<script type="text/javascript" src="test/metadata.js"></script>
<script type="text/javascript" src="jquery.example.js"></script>
Expand Down Expand Up @@ -238,4 +235,4 @@ <h3>JavaScript</h3>
});
</script>
</body>
</html>
</html>
18 changes: 9 additions & 9 deletions jquery.example.js
@@ -1,5 +1,5 @@
/*
* jQuery Form Example Plugin 1.4.3
* jQuery Form Example Plugin 1.5
* Populate form inputs with example text that disappears on focus.
*
* e.g.
Expand All @@ -11,7 +11,7 @@
* className: 'example_text'
* });
*
* Copyright (c) Paul Mucur (http://mucur.name), 2007-2008.
* Copyright (c) Paul Mucur (http://mudge.name), 2007-2011.
* Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
* licenses.
*
Expand All @@ -25,7 +25,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
(function($) {
(function($, undefined) {

$.fn.example = function(text, args) {

Expand Down Expand Up @@ -67,7 +67,7 @@
* values must be cleared on page unload to prevent them from
* being saved.
*/
$(window).unload(function() {
$(window).bind('unload.example', function() {
$('.' + o.className).val('');
});

Expand All @@ -78,7 +78,7 @@
* parents of example fields but this meant that a page with
* multiple forms would not work correctly.
*/
$('form').submit(function() {
$('form').bind('submit.example', function() {

/* Clear only the fields inside this particular form. */
$(this).find('.' + o.className).val('');
Expand All @@ -99,7 +99,7 @@
*
* Many thanks to Klaus Hartl for helping resolve this issue.
*/
if (!$this.attr('defaultValue') && (isCallback || $this.val() == o.example))
if (!$this.attr('value') && (isCallback || $this.val() == o.example))
$this.val('');

/* Initially place the example text in the field if it is empty
Expand All @@ -121,7 +121,7 @@
* seems wasteful and can stop people from using example values as real
* input.
*/
$this.focus(function() {
$this.bind('focus.example', function() {

/* jQuery 1.1 has no hasClass(), so is() must be used instead. */
if ($(this).is('.' + o.className)) {
Expand All @@ -131,14 +131,14 @@
});

/* Detect a change event to the field and remove the example class. */
$this.change(function() {
$this.bind('change.example', function() {
if ($(this).is('.' + o.className)) {
$(this).removeClass(o.className);
}
});

/* Make the example text reappear if the input is blank on blurring. */
$this.blur(function() {
$this.bind('blur.example', function() {
if ($(this).val() == '') {
$(this).addClass(o.className);

Expand Down
8 changes: 3 additions & 5 deletions jquery.example.min.js
@@ -1,5 +1,5 @@
/*
* jQuery Form Example Plugin 1.4.3
* jQuery Form Example Plugin 1.5
* Populate form inputs with example text that disappears on focus.
*
* e.g.
Expand All @@ -11,7 +11,7 @@
* className: 'example_text'
* });
*
* Copyright (c) Paul Mucur (http://mucur.name), 2007-2008.
* Copyright (c) Paul Mucur (http://mudge.name), 2007-2011.
* Dual-licensed under the BSD (BSD-LICENSE.txt) and GPL (GPL-LICENSE.txt)
* licenses.
*
Expand All @@ -25,6 +25,4 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
(function(a){a.fn.example=function(e,g){var d=a.isFunction(e),f=a.extend({},g,{example:e});return this.each(function(){var c=a(this),b=a.metadata?a.extend({},a.fn.example.defaults,c.metadata(),f):a.extend({},a.fn.example.defaults,f);if(!a.fn.example.boundClassNames[b.className]){a(window).unload(function(){a("."+b.className).val("")});a("form").submit(function(){a(this).find("."+b.className).val("")});a.fn.example.boundClassNames[b.className]=true}if(!c.attr("defaultValue")&&(d||c.val()==b.example))c.val("");
if(c.val()==""&&this!=document.activeElement){c.addClass(b.className);c.val(d?b.example.call(this):b.example)}c.focus(function(){if(a(this).is("."+b.className)){a(this).val("");a(this).removeClass(b.className)}});c.change(function(){a(this).is("."+b.className)&&a(this).removeClass(b.className)});c.blur(function(){if(a(this).val()==""){a(this).addClass(b.className);a(this).val(d?b.example.call(this):b.example)}})})};a.fn.example.defaults={className:"example"};a.fn.example.boundClassNames=[]})(jQuery);

(function(a,b){a.fn.example=function(b,c){var d=a.isFunction(b),e=a.extend({},c,{example:b});return this.each(function(){var b=a(this);if(a.metadata)var c=a.extend({},a.fn.example.defaults,b.metadata(),e);else var c=a.extend({},a.fn.example.defaults,e);a.fn.example.boundClassNames[c.className]||(a(window).bind("unload.example",function(){a("."+c.className).val("")}),a("form").bind("submit.example",function(){a(this).find("."+c.className).val("")}),a.fn.example.boundClassNames[c.className]=!0),!b.attr("value")&&(d||b.val()==c.example)&&b.val(""),b.val()==""&&this!=document.activeElement&&(b.addClass(c.className),b.val(d?c.example.call(this):c.example)),b.bind("focus.example",function(){a(this).is("."+c.className)&&(a(this).val(""),a(this).removeClass(c.className))}),b.bind("change.example",function(){a(this).is("."+c.className)&&a(this).removeClass(c.className)}),b.bind("blur.example",function(){a(this).val()==""&&(a(this).addClass(c.className),a(this).val(d?c.example.call(this):c.example))})})},a.fn.example.defaults={className:"example"},a.fn.example.boundClassNames=[]})(jQuery)
2 changes: 1 addition & 1 deletion test/index.html
Expand Up @@ -15,7 +15,7 @@ <h2 id="banner"></h2>
<div id="results"></div>
<div id="main"></div>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="metadata.js"></script>

<!-- Include the library twice. -->
Expand Down

0 comments on commit b8c1bdb

Please sign in to comment.