Skip to content

Work with noConflict and bug on Android 2.3 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 71 additions & 67 deletions jquery.labelify.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,83 +19,87 @@
*
* @example $('input').labelify('hasLabel'); // return true if the field has a label
*/
jQuery.fn.labelify = function(settings) {
// if the element has a label, return true when 'hasLabel' is passed as an arg
if (typeof settings === 'string' && settings === 'hasLabel') {
return $(this).data('hasLabel');
}
(function($) {
$.fn.labelify = function(settings) {
// if the element has a label, return true when 'hasLabel' is passed as an arg
if (typeof settings === 'string' && settings === 'hasLabel') {
return $(this).data('hasLabel');
}

settings = jQuery.extend({
text: 'title',
labeledClass: ''
}, settings);
settings = $.extend({
text: 'title',
labeledClass: ''
}, settings);

// Compatibility with version 1.3 and prior (double-ls)
if (settings.labelledClass) { settings.labeledClass = settings.labelledClass; }
// Compatibility with version 1.3 and prior (double-ls)
if (settings.labelledClass) { settings.labeledClass = settings.labelledClass; }

var showLabel, hideLabel,
lookups, lookup,
$labelified_elements;
var showLabel, hideLabel,
lookups, lookup,
$labelified_elements;

lookups = {
title: function(input) {
return $(input).attr('title');
},
label: function(input) {
return $("label[for=" + input.id +"]").text();
}
};
lookups = {
title: function(input) {
return $(input).attr('title');
},
label: function(input) {
return $("label[for=" + input.id +"]").text();
}
};

$labelified_elements = $(this);
$labelified_elements = $(this);

showLabel = function(el){
$(el).data('value', el.value);
el.value = $(el).data("label");
$(el).addClass(settings.labeledClass).data('hasLabel', true);
};
hideLabel = function(el){
el.value = $(el).data('value');
$(el).removeClass(settings.labeledClass).data('hasLabel', false);
};
showLabel = function(el){
$(el).data('value', el.value);
$(el).data('maxlength', $(el).attr('maxlength')).removeAttr('maxlength');
el.value = $(el).data("label");
$(el).addClass(settings.labeledClass).data('hasLabel', true);
};
hideLabel = function(el){
$(el).attr('maxlength', $(el).data('maxlength'));
el.value = $(el).data('value');
$(el).removeClass(settings.labeledClass).data('hasLabel', false);
};

return $(this).each(function() {
var $item = $(this),
removeValuesOnExit;
return $(this).each(function() {
var $item = $(this),
removeValuesOnExit;

if (typeof settings.text === 'string') {
lookup = lookups[settings.text]; // what if not there?
} else {
lookup = settings.text; // what if not a fn?
}
if (typeof settings.text === 'string') {
lookup = lookups[settings.text]; // what if not there?
} else {
lookup = settings.text; // what if not a fn?
}

// bail if lookup isn't a function or if it returns undefined
if (typeof lookup !== "function" || !lookup(this)) { return; }
// bail if lookup isn't a function or if it returns undefined
if (typeof lookup !== "function" || !lookup(this)) { return; }

$item.bind('focus.label',function() {
if (this.value === $(this).data("label")) { hideLabel(this); }
}).bind('blur.label',function(){
if (this.value == '') { showLabel(this); }
});
$item.data('label', lookup(this).replace(/\n/g,'')) // strip label's newlines
$item.data('value', this.value); // initialise remembered value

removeValuesOnExit = function() {
$labelified_elements.each(function(){
$item.bind('focus.label',function() {
if (this.value === $(this).data("label")) { hideLabel(this); }
}).bind('blur.label',function(){
if (this.value == '') { showLabel(this); }
});
};

$item.parents("form").submit(removeValuesOnExit);
$(window).unload(removeValuesOnExit);

if (this.value !== this.defaultValue || this.defaultValue != '') {
// user already started typing; don't overwrite their work!
// also, if a value is already set in the field, don't replace it
// with a label
return;
}
$item.data('label', lookup(this).replace(/\n/g,'')) // strip label's newlines
$item.data('value', this.value); // initialise remembered value

removeValuesOnExit = function() {
$labelified_elements.each(function(){
if (this.value === $(this).data("label")) { hideLabel(this); }
});
};

// set the defaults
showLabel(this);
});
};
$item.parents("form").submit(removeValuesOnExit);
$(window).unload(removeValuesOnExit);

if (this.value !== this.defaultValue || this.defaultValue != '') {
// user already started typing; don't overwrite their work!
// also, if a value is already set in the field, don't replace it
// with a label
return;
}

// set the defaults
showLabel(this);
});
};
}(jQuery));