Skip to content

Instantly share code, notes, and snippets.

@erikssonk
Created March 23, 2012 13:47
Show Gist options
  • Select an option

  • Save erikssonk/2170755 to your computer and use it in GitHub Desktop.

Select an option

Save erikssonk/2170755 to your computer and use it in GitHub Desktop.
defaultValues
// Usage: defaultValues.init(Element to bind);
var defaultValues = {
isOn : false, // Needed to se state of object
isBind : false, // Check if the elm's have the blur and focus binded
ThisElm : false, // Default to false.
// Init function
init: function($elm) {
// Check if it's off and not bound.
if (!this.isOn && !this.isBind) {
this.ThisElm = $elm;
this.isOn = true;
this.bind();
// It's on but not bound.. bind it!
} else if ($this.isBind) {
this.bind();
}
},
// Bind function to elm
bind : function() {
if (this.isOn && !this.IsBind) {
this.ThisElm.live('focus',function() {
theTemp = $(this).attr('temp'); //The temp value - if it exists
var theValue = $(this).val(); //The value of the input
//Now either the temp value is undefined (first time) or the temp and value are the same - 'Search' in any given language
if(theTemp == undefined || theTemp == theValue){
$(this).attr('temp', theValue); //Set the temp to the same as value
$(this).val(''); //Empty the value attribute to allow typing
}
//See, if the value is NOT equal to the temp value and the temp value is NOT undefined, it means the user has typed something. Let them keep it.
});
this.ThisElm.live('blur',function() {
//if the box is empty - let's populate the value with our saved standard text in the temp attribute
if ($(this).attr('value') == "") {
var theValue = $(this).attr('temp');
$(this).attr('value',theValue);
}
//nothing else - if the user has typed something, let them keep it
});
}
},
// Unbind it.
unbind : function() {
if (this.isBind) {
this.ThisElm.unbind('focus');
this.ThisElm.unbind('blur');
}
},
// Destroy this object
destroy : function() {
if (this.isOn) {
if (this.isBind) {
this.unbind();
}
this.isOn = false;
this.ThisElm = false;
return true;
} else {
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment